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

Asked

Viewed 83 times

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 records from 2017 except SHIPPING AND TAXES! I have it:

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

But this way is coming all records 2017, including freight and taxes.

2 answers

7

Use like this:

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

The reason your code went wrong is because you were using the OR. If you were FRETE, we have to FRETE is different from IMPOSTO, then it would be true. If it were IMPOSTO, we have to IMPOSTO is different from FRETE, then it would also be true.

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

-2

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

Browser other questions tagged

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