Select another field in mysql if field is set to null?

I want select displayname as username

if username = null and if username is NOT NULL then select username as username

eg.

id username displayname
1   xyz       NULL
2   NULL      abc

      

How can I do this in one select declaration ... Thanks

+2


source to share


1 answer


coalesce (username, dispalyname) should help you.

eg:.



select coalesce(username, dispalyname) from users
where ...

      

+5


source







All Articles