postgresql 使用ORDER BY实现LESS THAN某个ID

gjmwrych  于 12个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(152)

继续我之前的问题。here
使用ORDER BY将某行移动到所有行的顶部之后

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

字符串
如果我想让other comment从小于id = 5的例子开始怎么办?

id      comment
other comment         149    my comment
other comment         148    my comment
other comment         147    my comment
my comment       =>    4     other comment -- start from less than id = 5
other comment          3     other comment
my comment             2     other comment
my comment             1     other comment

1qczuiv0

1qczuiv01#

应翻译为:

SELECT *
FROM   comments
WHERE (id < 5 or comment = 'my comment')
ORDER  BY comment <> 'my comment', id DESC;

字符串
fiddle
相关:

  • 将具有给定值的行排序在所有其他行之前
  • PostgreSQL:按列排序,具有特定的NON-NULL值LAST

相关问题