将具有给定值的行排序在所有其他行之前

问题描述 投票:0回答:1

我找到了相关答案这里

就我而言,我有一个表格列

comment
。如何将“我的评论”移至顶部,然后按
id
排序?

other comment          my comment
other comment          my comment
other comment          my comment
my comment       =>    other comment
other comment          other comment
my comment             other comment
my comment             other comment
sql postgresql sql-order-by
1个回答
0
投票

假设您的目标是:

“将‘我的评论’排序在其他评论之前,然后按

id
排序”

SELECT *
FROM   comments
ORDER  BY comment <> 'my comment', id;

之所以有效,是因为

boolean
false
(想想 0)在
true
(想想 1)之前排序。
或者你的目标是:

“将‘我的评论’排序在其他评论之前,然后按字母顺序对评论进行排序,然后按

id
排序”

SELECT *
FROM   comments
ORDER  BY comment <> 'my comment', comment, id;

小提琴

参见(包含更多链接):

© www.soinside.com 2019 - 2024. All rights reserved.