Skip to content Skip to sidebar Skip to footer

What Does It Happen When Using Multiple Case ... When Statements In The Same Sql Query?

After my previous question, I have an SQL query like the following: SELECT * WHERE ... ORDER BY CASE user_id WHEN 34 THEN 1 ELSE 2 END, CASE status WHEN 'active' THEN 1 ELS

Solution 1:

First will come all records where user_id =34 with "active" status, then records with user_id=34 and status not "active", then records where user_id not equal 34 and status equals "active" , finally all remaining records where status is not "active"

Solution 2:

Order by are done in the order that you write them.

Your record will be ordered:

[first] user_id = 34, status = 'active' user_id = 34, status <> 'active' user_id <> 34, status = 'active' user_id <> 34, status <> 'active' [last]

Post a Comment for "What Does It Happen When Using Multiple Case ... When Statements In The Same Sql Query?"