SQL - How to select or delete a column with value equal to null in a query select

Asked

Viewed 4,608 times

4

I have a table in a Mysql comic that has 5 fields. This would be a model query:

select * from myTable where campo1= 4 and campo2=1 and campo3 =7.

However field 3 may have null value and I would need to select or exclude combinations that involve this value.

Example1:

select * from myTable where campo1= 4 and campo2=1 and campo3 = null.

Exemplo2:

select * from myTable where campo1= 4 and campo2=1 and campo3 <> null.

The problem is that these two examples do not work. I’ve tried to use "null" and 'null' but I can’t select.

Any idea?

2 answers

3

In case the campo3 = null and not want to show it:

select * from myTable where campo1= 4 and campo2=1 where campo3 is not null

In case you want to show only when the campo3<>null:

select * from myTable where campo1= 4 and campo2=1 where campo3 is null

2


Instead of using campo = null try to do it that way:

WHERE campo IS NULL

In the Mysql this expression tests if the field is null. To do the reverse just use the IS NOT NULL.

See working on Sqlfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.