SQL脚本排序

时间:2011-05-26 06:47:10

标签: mysql sql sql-order-by

我的脚本有:

  select * from users where...

where子句有两个部分,需要以这种方式排序:

  1. where description = "query"

  2. where description = "%query%"

有没有办法将description =“query”作为1或description =“%query%”作为2并对其进行排序?

1 个答案:

答案 0 :(得分:2)

使用:

SELECT a.*,
       1 AS sort
  FROM USERS a
 WHERE a.description LIKE 'query'
UNION ALL
SELECT b.*,
       2 AS sort
  FROM USERS b
 WHERE b.description LIKE '%query%'
ORDER BY sort