Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: select multiple ids with manual order

Tags:

sql

select

sqlite

I want to select multiple Ids but in my order. for example:

SELECT * FROM mytable WHERE id IN (3,1,4)

I want the order to be: 3 1 4 same as my query.

Any idea?

like image 792
user1789764 Avatar asked Dec 07 '25 19:12

user1789764


1 Answers

You can use a case statement for your order by.

SELECT * FROM mytable WHERE id IN (3,1,4)
ORDER BY 
CASE 
WHEN id = 3 THEN 1
WHEN id = 1 THEN 2
WHEN id = 4 THEN 3
END
like image 65
FuzzyTree Avatar answered Dec 10 '25 09:12

FuzzyTree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!