How do I disable auto ordering of SELECT statement in SQL query? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I disable auto ordering of SELECT statement in SQL query?

I want to fetch data from database in the order of given ids - IN(9,5,2,10) Problem: The result is fetched in the ascending order Also I don't want the results in descending order. Example: $query = "SELECT * FROM products WHERE id IN(9,5,2,10) So first, data of starting parameter 9 should be fetched in the client computer then 5,2 then at last , id 10. Help is highly appreciated!

11th May 2022, 10:03 AM
Coding San
Coding San - avatar
8 Answers
+ 2
Coding San what I mean by this is that from you above mentioned query can be repeated for each individual id.
11th May 2022, 3:42 PM
JaScript
JaScript - avatar
+ 1
You can select each one id once. … where id=9 … where id=5 etc.
11th May 2022, 11:00 AM
JaScript
JaScript - avatar
11th May 2022, 3:47 PM
Simon Sauter
Simon Sauter - avatar
+ 1
You can select specific portion of data (2,5,9,10) and then sort them using backend language like php.
13th May 2022, 4:11 AM
Tom Mac
+ 1
Coding San if the ordering is not completely ad-hoc, but based on actual field values, then you can specify ordering even if you don't select the columns that you order by. For example: SELECT name FROM people WHERE id IN(2, 5, 9) ORDER BY age
13th May 2022, 7:15 AM
Tibor Santa
Tibor Santa - avatar
0
JaScript this is actually for a product CART(e-commerce). Can you clarify your answer in more detail?
11th May 2022, 11:10 AM
Coding San
Coding San - avatar
0
Oboh Boniface Emesealu you can enter multiple conditions after WHERE clause and combine them with AND, OR and use parentheses as needed. The conditions can refer to different columns.
13th May 2022, 7:12 AM
Tibor Santa
Tibor Santa - avatar
0
Thank you all for the replies. I will implement all your suggestions and get back to you
13th May 2022, 9:44 AM
Oboh Boniface Emesealu
Oboh Boniface Emesealu - avatar