1
I have a database in Mysql designed like this:
id, nome
At the following values:
1, Banana
2, Maçã
3, Uva
4, Laranja
I have a select like this:
select * FROM table WHERE id = 2 OR id = 1 OR id = 4;
The answer I get is ordered by the ID, because I didn’t put any ORDER BY:
1, Banana
2, Maçã
4, Laranja
But I need the order to be by the condition written in the WHERE:
2, Maçã
1, Banana
4, Laranja
How to do it? Remembering that the example is simplified, focusing only on my doubt.
If you know exactly which Ids to search for you can just pick based on the ID as an input in the application (in PHP for example) instead of order in the query.
– Guilherme Nascimento
Disregarding search for performance or best practices in the query, UNION could not help? Something like: (SELECT * FROM fruit WHERE id = 2) UNION (SELECT * FROM fruit WHERE id = 1) UNION (SELECT * FROM fruit WHERE id = 4).
– Jean Barbosa