开发者

Select with set order in mysql

I have a query like this:

SELECT title,id FROM table1 WHERE id IN ('2','7','4','10')

The result set is ordered by id by default, but I need it in the exact order of number开发者_Python百科s in above set.

How can I achieve this?


The FIELD() function should be able to do this:

SELECT
    title, id
FROM
    table1
WHERE
    id IN ('2', '7', '4', '10')
ORDER BY FIELD(id, '2', '7', '4', '10')

See also MySQL sort after argument in IN().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜