Search code examples
sqlpostgresqlsql-order-by

Implement LESS THAN a certain id with ORDER BY


Continue from my question before. here

After move some row to the top of all rows using ORDER BY

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

How if i want the other comment start from example like less than 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

Solution

  • Should translate to:

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

    fiddle

    Related: