Home  >  Article  >  Database  >  How to sort in a specific order in mysql

How to sort in a specific order in mysql

藏色散人
藏色散人forward
2021-07-16 15:29:484138browse

Mysql queries IN and sorts it in a specific order

When performing data query operations, sometimes we sort it in a specific order.

Suddenly seen on the Internet To a sorting method, record it for later use:

Normal in query

select * from score where id in(3,1,5);

How to sort in a specific order in mysql

We can see that the data is It came out, but it is not in the order we need.

So how to query so that the found data can be in the order of (3,1,5)?

Specific order sorting (field)

select * from score where id in(3,1,5) order by field(id,3,1,5);

How to sort in a specific order in mysql

The order found in this way is what we want.

Recommended learning: "mysql video tutorial"

The above is the detailed content of How to sort in a specific order in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:fengkui. If there is any infringement, please contact admin@php.cn delete