tags:

views:

32

answers:

3

Let's say I have this MySQL query:

SELECT * FROM A WHERE x='abc' OR y=0;

How can I prioritize the rows so that cases where x='abc' are ordered FIRST? If y=0 but x!='abc', I want those rows to come after cases where x='abc'.

Can this be accomplished with a simple ORDER BY clause?

Thanks!

+2  A: 
SELECT *
FROM A 
WHERE x='abc' 
    OR y=0
order by case when x='abc' then 0 else 1 end;
RedFilter
A: 

I would do it with a case statement:

SELECT *, 
    CASE WHEN column_one = 0 THEN 0 
         WHEN column_two = 'ADMIN' THEN 1 
    END AS multi_column 
FROM sometable
ORDER BY multi_column;
gabe
A: 

ORDER BY FIELD(x,'abc',x)

Mchl