[MDEV-16911] Query empty when in subquery field has null Created: 2018-08-07  Updated: 2018-08-29  Resolved: 2018-08-29

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.3.8
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Aleksey M. Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Ubuntu 18.04



 Description   

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
 
DROP TABLE IF EXISTS `a1`;
CREATE TABLE `a1` (
  `year` smallint(5) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `a1` (`year`) VALUES
(2014), (2015), (2016), (2017), (NULL);
 
DROP TABLE IF EXISTS `a2`;
CREATE TABLE `a2` (
  `year` smallint(5) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `a2` (`year`) VALUES
(2014), (2015), (2016), (2017), (2018);
 
SELECT * FROM `a2`
WHERE year NOT IN (SELECT year FROM a1);

Result is empty. Waiting "2018".



 Comments   
Comment by Elena Stepanova [ 2018-08-29 ]

Your table a1 has NULL among values, so the last query is effectively SELECT * FROM `a2` WHERE year NOT IN (2014,2015,2016,2017,NULL).
Rules for NULL as an operand are often found to be confusing, which is why they are usually explicitly documented. You can find information, for example, in the MariaDB KB: NOT IN, IN.
In short, with NULL among operands, most operators always return NULL, which for the WHERE clause means FALSE.

Generated at Thu Feb 08 08:32:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.