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

yoty66
  • 390
  • 2
  • 12

4 Answers4

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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));
Imran Khan
  • 311
  • 4
  • 9
  • 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 . – yoty66 Feb 08 '21 at 08:51
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
Akina
  • 39,301
  • 5
  • 14
  • 25
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')\;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786