站內搜尋

Monday, May 9, 2016

[MYSQL] 使用 ORDER BY 排序時,讓特定對象排在最上面的方法 (使用 CASE)

uid | joindate
A01 | 2015-01-01
A02 | 2016-02-01
A03 | 2016-03-01

我們希望當A02這個User登入時,報表顯示如下
A02 | 2016-02-01
A01 | 2016-01-01
A03 | 2016-03-01

SQL語法如何下呢?...
SELECT *
FROM mytable
ORDER BY
  CASE
    WHEN uid = 'A02' THEN 0
    ELSE 1
  END,
  joindate

No comments:

Post a Comment