I have some data that looks like
name state
mary florida
mary NULL
john alabama
mary NULL
salt texas
I want to grab results that dont include mary's with a null state. When i try
select name, state from my table where (state is not null and name = 'mary')
This only gives me all the mary's that has a state connected to it.
what im looking for in the expected results is
name state
mary florida
john alabama
salt texas
Select all except name='mary' with state is NULL:
select name, state from my table where NOT (state IS NULL AND name = 'mary');
Or the same according to the De Morgan's law (the negation of a conjunction is the disjunction of the negations):
select name, state from my table where (state IS NOT NULL) OR (name != 'mary');
select name, state from my table where state is not null
The above query will provide you the expected results
and its a good practice to use coalesce function to the column which has null val es when you are not getting expected result
select name, state from my table where coalesce(state,'default') is not default;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.