Sort rows in Postgresql by a list of values
Use the POSITION function for strings:
SELECT *
FROM table
WHERE ID IN (1, 2, 3, 4, 5)
ORDER BY POSITION(id::text IN '2, 1, 5, 3, 4')
Cheers.
Written by Daniel Alejandro Gaytán Valencia
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Queries
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#