Home >Database >Mysql Tutorial >How to sort mysql in query operation
In mysql, you can use the "ORDER BY" clause with the SELECT statement in query to operate sorting. The syntax is "select*from table name where field value in (sort 1, sort 2...) order by field(field value, sort 1, sort 2...);".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
How to sort mysql in query operations
Let’s talk about the solution first:
select * from test where id in(3,1,5) order by field(id,3,1,5);
Maybe someone will pay attention to it
SQL: select * from table where id IN (3,6,9,1,2,5,8,7);
After taking out such a situation, in fact, the IDs are still sorted by 1,2,3,4,5,6,7,8,9,
But if we really want to press IN How to sort the order inside? Can SQL be completed?
Do I need to get it back and then foreach? In fact, mysql has this method
field function.
Function format: order by field(str,str1,str2,str3...)
str is the field, str1\str2\str3 is the specific column value
sql: select * from table where id IN (3,6,9,1,2,5,8,7) order by field(id,3,6,9,1,2,5,8,7);
Recommended learning: mysql video tutorial
The above is the detailed content of How to sort mysql in query operation. For more information, please follow other related articles on the PHP Chinese website!