ORDER BY in MySQL

Associate
Joined
8 Aug 2008
Posts
302
Is it possible to do a query so it ORDERS BY one column if it has a value and another if not?

Basically I want to pull out a list in the order of column A or B.

Now column A will always have a value, B not so.

Any thoughts?

Cheers
 
Is it possible to do a query so it ORDERS BY one column if it has a value and another if not?

Basically I want to pull out a list in the order of column A or B.

Now column A will always have a value, B not so.

Any thoughts?

Cheers

My thoughts are I don't really understand what your trying to do.

But here's what I *think* your trying to do.

(SELECT cols FROM TABLE WHERE A != nulll ORDER BY A) UNION
(SELECT cols FROM TABLE WHERE A=null ORDER BY B)
 
You should be able to do something like ORDER BY COALESCE(columnB, columnA)

That will use the value of columnB if it is not null and columnA if B is null and then order by that.
 
My thoughts are I don't really understand what your trying to do.

But here's what I *think* your trying to do.

(SELECT cols FROM TABLE WHERE A != nulll ORDER BY A) UNION
(SELECT cols FROM TABLE WHERE A=null ORDER BY B)

When dealing with nulls you should be using IS NULL or IS NOT NULL, not = NULL or != NULL.
 
Back
Top Bottom