Query SQL condition if

1

I have a problem in doing a query where I have 3 fields and only one of them is filled up that no longer shows this result.

Can someone help me do this?

select * from tabelas where campo1 and campo2 and campo3
    
asked by anonymous 30.05.2014 / 17:24

2 answers

3

If you want everyone to be shown unless one is filled in, do this:

SELECT *
FROM tabelas
WHERE campo1 IS NULL
  AND campo2 IS NULL
  AND campo3 IS NULL

If you want to where everyone is filled use IS NOT NULL :

SELECT *
FROM tabelas
WHERE campo1 IS NOT NULL
  AND campo2 IS NOT NULL
  AND campo3 IS NOT NULL
    
30.05.2014 / 19:54
1

You can use the COALESCE function to identify if all fields are null (combined with is null ), or if any of them are filled ( is not null ):

select * from tabelas where coalesce(campo1, campo2, campo3) is null;

select * from tabelas where coalesce(campo1, campo2, campo3) is not null;

SQL Fiddle Example

    
30.05.2014 / 18:15