我有一张表格如下,
表1
当我使用查询时
"SELECT cat_title FROM table1 WHERE cat_id IN(21,10,25,4)"
结果按顺序
cat_title
银行
美容院
汽车
住宿
但我需要查询中给出的结果,如
cat_title
汽车
Beaty Parlour
住宿
银行
我该怎么办?
答案 0 :(得分:2)
你可以试试这个:
SELECT cat_title
FROM table1
WHERE cat_id IN(21,10,25,4)
ORDER BY (cat_id = 21) DESC, (cat_id = 10) DESC, (cat_id = 25) DESC, (cat_id = 4) DESC;
或
SELECT cat_title
FROM table1
WHERE cat_id IN(21,10,25,4)
ORDER BY FIND_IN_SET(cat_id, ('21,10,25,4'));
答案 1 :(得分:1)
SELECT cat_title FROM table1
Where cat_id IN(21,10,25,4)
ORDER BY (
CASE WHEN cat_id = 21 THEN 0
WHEN cat_id = 4 THEN 1
WHEN cat_id = 10 THEN 2
WHEN cat_id = 25 THEN 3
END
)