How to implement ternary conditional operator in MySQL

user1086355 picture user1086355 · Dec 14, 2011 · Viewed 42.8k times · Source

I want to implement ternary conditional operator in MySQL. I have a table in which one field id exist. Its value may be null. I want to display id in ternary conditional format like this:

select id = id == null ? 0 : id;

Is it possible in MySQL?

Answer

Dewasish Mitruka picture Dewasish Mitruka · Dec 14, 2011

Try this :

select if(Id is null, 0, id) as Id;