MySQL IS NULL / IS NOT NULL 行为不端?

use*_*259 18 mysql count

请看这张表:

mysql> desc s_p;

+-------------------------+------------------+------+-----+---------+----------------+    
| Field                   | Type             | Null | Key | Default | Extra          |
+-------------------------+------------------+------+-----+---------+----------------+
| id                      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| s_pid                   | int(10) unsigned | YES  | MUL | NULL    |                |
| sm_id                   | int(10) unsigned | YES  | MUL | NULL    |                |
| m_id                    | int(10) unsigned | YES  |     | NULL    |                |
| created                 | datetime         | YES  |     | NULL    |                |
| s_date                  | datetime         | YES  |     | NULL    |                |
| estimated_date          | datetime         | YES  | MUL | NULL    |                |
+-------------------------+------------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)

现在看看这些查询:

mysql> select count(*) from s_p where estimated_date is null;
+----------+
| count(*) |
+----------+
|   190580 |
+----------+
1 row in set (0.05 sec)

mysql> select count(*) from s_p where estimated_date is not null;
+----------+
| count(*) |
+----------+
|    35640 |
+----------+
1 row in set (0.07 sec)

mysql> select count(*) from s_p;
+----------+
| count(*) |
+----------+
|  1524785 |
+----------+
Run Code Online (Sandbox Code Playgroud)

上面的计数不匹配。而根据我的理解:

在没有 where 子句的情况下查询时,Count withIS NULL和 Count withIS NOT NULL应该等于 count。

知道这里发生了什么吗?

================================================== =

2012 年 2 月 17 日更新

从那以后,我发现很多人都在询问估计日期当前具有的值类型。这是答案:

mysql> select distinct date(estimated_date) from s_p;

+----------------------+
| date(estimated_date) |
+----------------------+
| NULL                 |
| 2012-02-17           |
| 2012-02-20           |
| 2012-02-21           |
| 2012-02-22           |
| 2012-02-23           |
| 2012-02-24           |
| 2012-02-27           |
| 2012-02-28           |
+----------------------+
9 rows in set (0.42 sec)
Run Code Online (Sandbox Code Playgroud)

正如您在上面看到的那样,estimated_date 要么具有 NULL 要么具有有效的日期时间值。没有零或空字符串“”。

如果estimated_date 上的索引有问题,这个(原始问题)会发生吗?

================================================== =

2012 年 2 月 18 日更新

这是 show create table 输出:

 | s_p | CREATE TABLE `s_p` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `s_id` int(10) unsigned DEFAULT NULL,
  `sm_id` int(10) unsigned DEFAULT NULL,
  `m_id` int(10) unsigned DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `estimated_date` datetime DEFAULT NULL,
   PRIMARY KEY (`id`),
   KEY `sm_id` (`sm_id`),
   KEY `estimated_date_index` (`estimated_date`) USING BTREE,
  ) ENGINE=InnoDB AUTO_INCREMENT=1602491 DEFAULT CHARSET=utf8 |
Run Code Online (Sandbox Code Playgroud)

同样,我只能在这里怀疑估计日期上的索引。

另外,mysql 服务器版本是 5.5.12。

ara*_*nid 7

你有一些零日期吗?0000-00-00 00:00:00MySQL 认为 的日期时间值同时满足is nullis not null

steve@steve@localhost > create temporary table _tmp (a datetime not null);
Query OK, 0 rows affected (0.02 sec)

steve@steve@localhost > insert into _tmp values ('');
Query OK, 1 row affected, 1 warning (0.00 sec)

Warning (Code 1264): Out of range value for column 'a' at row 1
steve@steve@localhost > select a from _tmp where a is null;
+---------------------+
| a                   |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

steve@steve@localhost > select a from _tmp where a is not null;
+---------------------+
| a                   |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

见:http : //bugs.mysql.com/bug.php?id=940

这被归类为“不是错误”。他们提出了一种解决方法:使用严格模式,这会将插入警告转换为错误。

说了这么多,仅此一项并不能解释您得到的结果的巨大变化(is nullis not null计数的总和应该超过不受限制的计数)......


Nav*_*mar 1

尝试查询

select * from s_p where estimated_date is null and estimated_date is not null limit 5;
Run Code Online (Sandbox Code Playgroud)

  • 上面的查询将显示行为不当的行,您可以从中找到解决方案。 (2认同)