Sample Header Ad - 728x90

Is it possible to sort by the sequence of input IDs?

2 votes
4 answers
6998 views
I want to find a user's favorite music list. First step, I query all id from user's favorite table. In the next step I query the music list by id from songs table. But now I want to sort rows by the timestamp column favorite.added_at desc. I do not want to join table, so I want to figure out if it's possible to sort by the input id in the in query. For example: -- the 3087 is first record select * from songs s where id in (3087,5122) -- the 5122 is first record select * from songs s where id in (5122,3087) I have tried in PostgreSQL 13, and the row with id = 3087 was always first. Seems like PostgreSQL does not fetch rows by the sequence of values in the IN list?
Asked by Dolphin (939 rep)
Sep 26, 2021, 04:53 AM
Last activity: Dec 9, 2024, 05:32 PM