0

这是一个查询:

SELECT * 
  FROM table
 WHERE id = 1
    OR id = 100
    OR id = 50

请注意,我按以下顺序提供了 ID:1,100,50。

我希望这些行按以下顺序返回:1,100,50。

目前,我回来了 1,50,100 - 基本上是按升序排列。假设表中的行也按升序插入。

4

5 回答 5

3

使用 MySQL 特定的FIND_IN_SET函数:

  SELECT t.* 
    FROM table t
   WHERE t.id IN (1, 100, 50)
ORDER BY FIND_IN_SET(CAST(t.id AS VARCHAR(8)), '1,100,50')
于 2013-01-31T03:15:38.227 回答
2

解决此问题的另一种方法是将列表放在子查询中:

select table.*
from table join
     (select 1 as id, 1 as ordering union all
      select 100 as id, 2 as ordering union all
      select 50 as id, 3 as ordering
     ) list
     on table.id = list.id
order by list.ordering
于 2013-01-31T03:01:29.553 回答
0

你可以这样做ORDER BY

ORDER BY
   id = 1 DESC, id = 100 DESC, id = 50 DESC

01在之前ORDER BY

于 2013-01-31T02:54:24.173 回答
0

试试这个

SELECT * 
FROM new
WHERE ID =1
OR ID =100
OR ID =50
ORDER BY ID=1 DESC,ID=100 DESC,ID=50 DESC ;

http://www.sqlfiddle.com/#!2/796e2/5

于 2013-01-31T03:03:05.113 回答
0

... WHERE id IN (x,y,x) ORDER BY FIELD (id,x,y,z)

于 2013-01-31T07:30:43.593 回答