How to query multiple conditions with multiple columns, like AND clause in AND clause.
Example data
| Name | Country | ZipCode | | -------- | -------------- | -------------- | | A | Italy | 2020 | | B | Japan | 1010 | | C | Canada | 3030 | | D | Japan | 1011 | | E | Japan | 1012 |
The result that I need is without..
- Country = Japan and ZipCode = 1010
- Country = Canada and ZipCode = 3030
| Name | Country | ZipCode | | -------- | -------------- | -------------- | | A | Italy | 2020 | | D | Japan | 1011 | | E | Japan | 1012 |
I try to write SQL like :
SELECT * FROM table WHERE ((Country <> 'Japan' AND ZipCode <> '1010') AND (Country <> 'Canada' AND ZipCode <> '3030'))
but it’s not correct. Any help please ?
Advertisement
Answer
Just use not
:
where not (country = 'Japan' and ZipCode = '1010' or Country = 'Canada' and ZipCode = '3030' )
You can expand this out of course:
where (country <> 'Japan' or ZipCode <> '1010') and (country <> 'Canada' or Zipcode <> '3030')
Note: This constructs will also filter out NULL
values. There are none in your sample data and they can both be tweaked to handle them if necessary.