SQL beginner here, trying to figure out how I can grab bothNULL andNOT NULL values from a table in MySQL. I've tried omitting specifying null and not null to see if that would grab both types, but no luck.
SELECT COUNT('') as cnt FROM returned_items ri LEFT JOIN returns r ON ri.return_id = r.return_id WHERE r.storenum IN (11)WithNULL andNOT NULL:
SELECT COUNT('') as cnt FROM returned_items ri LEFT JOIN returns r ON ri.return_id = r.return_id WHERE r.storenum IN (11) AND ri.disposition is NULL AND NOT NULLAny advice is greatly appreciated.
- 2Any other possible values other then null and not null? I think you just need to remove check for dispositionVinay Pandey– Vinay Pandey2016-10-31 21:24:59 +00:00CommentedOct 31, 2016 at 21:24
- 1Also "AND ri.disposition is NULL AND NOT NULL" is saying it must equal both NULL and NOT NULL which is not possible.Adrianopolis– Adrianopolis2016-10-31 21:26:04 +00:00CommentedOct 31, 2016 at 21:26
- 2Regardless of the contradiction the syntax should be AND ri.disposition is NULL AND ri.dispositionNOT NULL in this case the column has to be repeated in every conditionScaisEdge– ScaisEdge2016-10-31 21:26:57 +00:00CommentedOct 31, 2016 at 21:26
- When you do a
LEFT JOIN, conditions on the second table should normally be in theONclause, notWHERE. Otherwise, it will filter out any rows in the first table that have no match, becauser.storenumwill beNULLin those rows. So it becomes effectively the same asINNER JOIN.Barmar– Barmar2016-10-31 21:49:49 +00:00CommentedOct 31, 2016 at 21:49 - It's not clear what you're trying to do here. There nothing in the first query that checks
ri.disposition, so it should return both null and non-null values.Barmar– Barmar2016-10-31 21:50:44 +00:00CommentedOct 31, 2016 at 21:50
1 Answer1
I don't think you really need that condition since you are trying to get both havingNULL andNOT NULL ... means you are trying to get all the records and thus the condition makes no sense but you can have two different queries and perform aUNION ALL like (though not sure why you would do that)
SELECT COUNT(*) as cnt FROM returned_items ri LEFT JOIN returns r ON ri.return_id = r.return_id WHERE r.storenum = 11 AND ri.disposition is NULLUNION ALLSELECT COUNT(*) as cnt FROM returned_items ri LEFT JOIN returns r ON ri.return_id = r.return_id WHERE r.storenum = 11 AND ri.disposition is NOT NULLComments
Explore related questions
See similar questions with these tags.

