I want to get all profiles first that are for certain location(s):
SELECT * FROM profile
WHERE location IN ('a', 'b', 'c') OR isDefault=1
ORDER BY location IN ('a', 'b') DESC, -- put to the front if location in 'a','b'
isDefault DESC, -- for each of both groups, put default profiles before the others.
location ASC -- and sort each of the up to four groups by location.
This throws the error: "Incorrect syntax near the keyword 'IN'.". If I remove the order clause, results are returned.
What is wrong here?
You can use the ASC and DESC keywords to specify ascending (smallest value first) or descending (largest value first) order. The default order is ascending.
An ORDER BY clause in SQL specifies that a SQL SELECT statement returns a result set with the rows being sorted by the values of one or more columns. The sort criteria do not have to be included in the result set.
The difference between "order by" and "sort by" is that the former guarantees total order in the output while the latter only guarantees ordering of the rows within a reducer. If there are more than one reducer, "sort by" may give partially ordered final results.
ORDER BY: This keyword is used to sort the result-set in ascending or descending order. It sorts the records in ascending order by default. ASC or DESC is the keyword to sort the record in ascending or descending order respectively.
You could rewrite that to give an integer back which is sortable:
case when location IN ('a', 'b') then 0 else 1 end DESC
You can't use IN
in the ORDER BY
. Change it to:
ORDER BY location ASC, --will sort a, then b, then c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With