Home >Database >Mysql Tutorial >How to sort mysql in query operation

How to sort mysql in query operation

WBOY
WBOYOriginal
2022-01-20 15:04:167611browse

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...);".

How to sort mysql in query operation

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn