mysql 按照 id in () 排序 框架

shuke 1月前 17

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:

```sql

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)
全部楼主
返回