+ 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!
8 ответов
+ 2
Coding San what I mean by this is that from you above mentioned query can be repeated for each individual id.
+ 1
You can select each one id once.
… where id=9
… where id=5
etc.
+ 1
You can use CASE.
https://learnsql.com/blog/order-by-specific-value/
+ 1
You can select specific portion of data (2,5,9,10) and then sort them using backend language like php.
+ 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
0
JaScript this is actually for a product CART(e-commerce). Can you clarify your answer in more detail?
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.
0
Thank you all for the replies. I will implement all your suggestions and get back to you