halocursed
halocursed

Reputation: 2485

Select different field in mysql if a field is set to null?

I want to 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 that in one single select statement...Thanks

Upvotes: 2

Views: 518

Answers (1)

elder_george
elder_george

Reputation: 7879

coalesce(username, dispalyname) should help you.

E.g.:

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

Upvotes: 5

Related Questions