user5783530
user5783530

Reputation: 251

MySQL Query order by multiple columns

I have a rows with (firstname, lastname, phone, email). What I want to make is a query which sort first by concat(firstname, lastname) asc and the rest rows, where the firstname and lastname are null have to come after the sorted by concat(firstname, lastname) ascand to be sorted depending on phone/email (if phone is null sort by email, if email is null sort by phone). Any suggestion can help me?

Upvotes: 0

Views: 64

Answers (2)

Sushil Mate
Sushil Mate

Reputation: 583

select * from table order by concat(firstname, lastname) asc,  (firstname is null and lastname is null) asc, coalesce(phone, email)

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You can put these rules into the order by. I think these are:

order by (firstname is null and lastname is null) asc,
         concat(firstname, lastname),
         coalesce(phone, email)

I am not sure if the first condition should be and or or.

Upvotes: 3

Related Questions