我有1个表格主题。
我想以这种方式订购行。
首先获取pinned = 1
按id ASC
第二个是由pinned = 0
created_ts DESC
的行
代码:
SELECT topics
WHERE category_id = :i AND deleted = :d
ORDER BY pinned ASC, created_ts DESC
注意:我错误地在表格中切换了列(edited_by< => edited_ts)
答案 0 :(得分:3)
如果其他列符合某些标准,您应该能够使用CASE
仅按列排序:
SELECT id, title, ...
FROM topics
WHERE category_id = :i AND deleted = :d
ORDER BY pinned DESC, -- first 1 then 0
CASE WHEN pinned = 1 THEN id ELSE 0 END, -- ignore for pinned=0
created_ts DESC
答案 1 :(得分:0)
这样的事情怎么样:
SELECT ...
ORDER BY pinned DESC,
CASE WHEN pinned = 1
THEN id
ELSE -created_ts
DESC