Order by another column if the column is null in mysql query statment

Votes : 3.3
227
My site user addresses have stored in a table. A user has default address and also any user can has a working address.
id user_id default_address default_city work_address work_city
1 2 No. 16D Manamulla Wes, Panadura, Sri Lanka Panadura No. 16D Manamulla Wes, Panadura, Sri Lanka Panadura
1 3 No. 1458B Wijerama Mawatha, Colombo 07, Sri Lanka Colombo NULL NULL
I want order by work_city column. if any work_city column value is null , these records are filter by default_city column. How can i do this?

Your Answer

1 Answer

answered
try this, select * from table_name order by ifnull(column1,column2); Table has been order by column1, But order by column2 If column1 is empty.

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila