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

1

I have a table in a MySQL bd that has 5 fields. This would be a template query:

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

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

Example 1:

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

Example2:

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

The problem is that these two examples do not work. I've already tried using "null" and 'null' but I can not select.

Any ideas?

    
asked by anonymous 24.05.2016 / 21:52

2 answers

1

Instead of using campo = null try doing this:

WHERE campo IS NULL

No Mysql o this expression tests whether the field is null . To do the reverse, just use IS NOT NULL .

See working in SQLFiddle

    
24.05.2016 / 22:02
2

In the case of campo3 = null and do 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 campo3<>null :

select * from myTable where campo1= 4 and campo2=1 where campo3 is null
    
02.06.2016 / 18:28