1

I want to query over two columns as 2-tuples , where one can be null.

example :

    +-------------------------+
    |   first    |     last   |
    +-------------------------+
    |   Homer    |     NULL   |
    |   Marge    |    Simpson |
    +-------------------------+

Something like:

Select * from cartoons where (first, last ) in ((Homer ,NULL  ), ( Marge ,Simpson) ) ;

(Which should retrieve both Homer's and Marge's rows)

I am using the following query for test:

select  ('Homer', null)  in (('Homer',  null));

which returns null.

Reference to related questions :

SQL version : 5.7.12

4
  • why don't change those null value with IFNULL() to avoid null problem? or is there any limitation so you can't change the data at all? Commented Feb 8, 2021 at 8:35
  • What is your version of MySql? Commented Feb 8, 2021 at 8:49
  • added to question Commented Feb 8, 2021 at 8:53
  • Try: select ('Homer', coalesce(null, 'null')) in (('Homer', coalesce(null, 'null'))); Commented Feb 8, 2021 at 12:42

4 Answers 4

0

You can't use the tuple syntax in this case, which uses equality comparisons under the hood. Instead, use the longer version:

SELECT *
FROM cartoons
WHERE first = 'Homer' AND last IS NULL OR first = 'Marge' AND last = 'Simpson';

Just for reference, here is what your current query is actually saying:

SELECT *
FROM cartoons
WHERE first = 'Homer' AND last = NULL OR first = 'Marge' AND last = 'Simpson';

Note that comparing a column directly to NULL is semantically incorrect.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank u for your answer . In my real life use case Im getting a long array of these 2-tuples . So writing this as a very long query is really not optimal solution .
You need parentheses
@Strawberry You need to re-read the precedence rules of SQL. AND has greater precedence than OR, so the logic as already written above is correct.
I can see this going very wrong very quickly
0

Try This:

SELECT * FROM cartoons WHERE (first in (Array1)  AND last IS NULL) OR (last in (Array2)  AND first IS NULL) OR (first in (Array1)  AND last in (Array2));

1 Comment

Thank u for your answer . But tough very creative this isn't equivalent to searching by tuples . Since a ('Marge' , null) tuple would also be part of the result set .
0
SELECT * 
FROM cartoons 
JOIN ( SELECT 'Homer' first, NULL last
       UNION ALL
       SELECT 'Marge', 'Simpson' ) criteria ON cartoons.first <=> criteria.first
                                           AND cartoons.last  <=> criteria.last

Comments

0

Use NULL-safe comparisons, which in MySQL uses <=>. Assuming that only the second column can be NULL:

Select c.*
from cartoons c
where (first = 'Homer' and last <=> NULL) or
      (first = 'Marge' and last <=> 'Simpson')\;

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.