我有一个像下面这样的查询。
SELECT name, id, note_date, note
FROM example
ORDER BY name, note_date
假设我有一个名为“John Smith”的用户。我希望查询按原样工作,但我希望 John Smith 的所有条目都违反顺序并显示在列表顶部。
我只是想知道除了 JOIN 之外是否有更简单或更合适的方法来执行此操作(如下所示)。
此外,如果重要的话,“John Smith”在任何给定时间可能有也可能没有条目。
谢谢!
喜欢:
SELECT name, id, note_date, note
FROM example
WHERE name = "John Smith"
ORDER BY note_date
JOIN
SELECT name, id, note_date, note
FROM example
WHERE name != "John Smith"
ORDER BY name, note_date
最佳答案
SELECT name,
id,
note_date,
note,
CASE
WHEN name= 'John Smith'
THEN 0
ELSE 1
END AS OrderStatement
FROM example
ORDER BY OrderStatement, name, note_date
您只需再创建一列来生成自定义订单,并将其与其他列一起在 ORDER BY 子句中使用。
关于MySQL 对订单进行异常(exception)处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45906592/