SELECT * FROM cms_product WHERE id IN (3,22,1);
结果顺序等于“ORDER BY id ASC”,因此在示例中返回记录1,3,22.如何以IN子句中输入的确切方式对它们进行排序?订购为3,1?谢谢.
最佳答案
众多选择 –
原文链接:https://www.f2er.com/mysql/433854.html案件:
首选,是ANSI-92,它可以移植到其他数据库.
SELECT *
FROM cms_product
WHERE id IN (3,1)
ORDER BY CASE id
WHEN 3 THEN 1
WHEN 22 THEN 2
WHEN 1 THEN 3
END
FIND_IN_SET:
SELECT *
FROM cms_product
WHERE id IN (3,1)
ORDER BY FIND_IN_SET(id,'3,1');
领域:
SELECT *
FROM cms_product
WHERE id IN (3,1)
ORDER BY FIELD(id,1);
参考:
> CASE
> FIELD
> FIND_IN_SET