0

SQL beginner here, trying to figure out how I can grab both NULL and NOT 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)

With NULL and NOT 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 NULL

Any advice is greatly appreciated.

6
  • 2
    Any other possible values other then null and not null? I think you just need to remove check for disposition Commented Oct 31, 2016 at 21:24
  • 1
    Also "AND ri.disposition is NULL AND NOT NULL" is saying it must equal both NULL and NOT NULL which is not possible. Commented Oct 31, 2016 at 21:26
  • 2
    Regardless 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 condition Commented Oct 31, 2016 at 21:26
  • When you do a LEFT JOIN, conditions on the second table should normally be in the ON clause, not WHERE. Otherwise, it will filter out any rows in the first table that have no match, because r.storenum will be NULL in those rows. So it becomes effectively the same as INNER JOIN. Commented Oct 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. Commented Oct 31, 2016 at 21:50

1 Answer 1

2

I don't think you really need that condition since you are trying to get both having NULL and NOT 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 a UNION 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 NULL
UNION ALL
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 NOT NULL
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.