17

I have 2 columns in mysql table: a and b. a is allways string value and b is sometimes a string value and sometimes it is null.

How to construct a mysql SELECT so that the b would be taken if it is not null and a would be taken otherwise.

I tried to make some magic with concat and if...then with no success...

UPDATE - To extend my question, is there a function that would work like Ifnull but would work for null AND empty values?

3 Answers 3

24

Use IFNULL(b, a).

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

This is a MySQL specific function. You can also use COALESCE in the same way. This will work in more databases but it has slightly worse performance in MySQL than IFNULL.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I didn't find this function mysqlf.
3

IFNULL(b, a) ?

Comments

3
IF(expr1,expr2,expr3)

If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.