mysql 按照 id in () 排序 框架

shuke 1月前 26

To sort MySQL results based on a specific order of IDs using the `IN` clause, you can use the `FIELD()` function in the `ORDER BY` clause. Here's an example query:


SELECT * FROM your_table

WHERE id IN (2, 5, 1, 3)  -- Specify the IDs in the desired order

ORDER BY FIELD(id, 2, 5, 1, 3);


In this query:

- Replace `your_table` with the actual table name.

- Specify the IDs inside the `IN` clause in the desired order.

- Use the `FIELD()` function in the `ORDER BY` clause to sort the results based on the specified order of IDs.

最新回复 (0)