Skip to content
Advertisement

MYSQL – How to check if a column has value or is it null and another column has value like 10

Lets say i have a table with 3 columns id, user and status. I need to find rows which have status as 1 or null and user = ‘someName’

I am lookin for a condition like this: if (user == ‘name’ && status == 1 || status == null)

How do i do this in mysql

Advertisement

Answer

Just:

WHERE user = 'someName' AND (status = 1 OR status IS NULL)

This can be simplified as the shorter (but possibly less efficient)

WHERE user = 'someName' AND IFNULL(status, 1) = 1

Note that this does not exactly the same thing as your pseudo-code:

user == 'name' && status == 1 || status == null

Is equivalent to:

( user == 'name' && status == 1 ) || status == null

Which would translate as:

WHERE (user = 'someName' AND status = 1) OR status IS NULL

My understanding of your question, however, is that you meant the first above conditions.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement