Set of WHERE, AND, and OR Conditions in SQL does not produce the expected result

1

I'm trying to get some records with a very simple syntax, but it's not working. I'm doing something wrong and I'm not realizing it. Can someone lend a hand?

I need to fetch all 2017 records except FREIGHT AND TAXES! I have this:

SELECT * 
FROM table_01 
WHERE data_reg LIKE '2017%' 
  AND (Desc <> 'FRETE' OR Desc <> 'IMPOSTO');

But that way all the 2017 registrations, including freight and taxes, are coming.

    
asked by anonymous 30.06.2017 / 18:11

2 answers

7

Use like this:

SELECT *
FROM table_01
WHERE data_reg LIKE '2017%'
AND Desc <> 'FRETE'
AND Desc <> 'IMPOSTO';

The reason for going wrong with your code is because you used the OR. If it were FRETE , we have that FRETE is different from IMPOSTO , then it would be true. If it were IMPOSTO , we have that IMPOSTO is different from FRETE , then it would also be true.

When using AND, we have that the record must be different from FRETE and also other than IMPOSTO .

    
30.06.2017 / 18:19
-2

You can use not in also, example: and Desc not in ('FRETE','IMPOSTO');

    
30.06.2017 / 18:45