在示例中我们也可以看到两个语句的结果实际上并不一致。mysql> show create table baguai_f \G
*************************** 1. row ***************************
Table: baguai_f
Create Table: CREATE TABLE `baguai_f` (
`id` int(11) DEFAULT NULL,
`a` varchar(20) DEFAULT NULL,
`b` varchar(20) DEFAULT NULL,
`c` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select * from baguai_f ;
+------+------+------+------+
| id | a | b | c |
+------+------+------+------+
| 1 | g | g | NULL |
| 1 | g1 | g1 | g1 |
| 3 | g2 | g2 | g2 |
| 4 | g | g | NULL |
| 5 | g | g | NULL |
| 6 | g3 | g3 | g3 |
+------+------+------+------+
6 rows in set (0.00 sec)
mysql> desc select count(*) from baguai_f where b='g';
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | baguai_f | NULL | ALL | NULL | NULL | NULL | NULL | 6 | 16.67 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> desc select count(c) from baguai_f where b='g';
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | baguai_f | NULL | ALL | NULL | NULL | NULL | NULL | 6 | 16.67 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> select count(*) from baguai_f where b='g';
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select count(c) from baguai_f where b='g';
+----------+
| count(c) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
这种不一致来自于 b=’g’ 的 c 列中都是 NULL 值,因此 count(c) 返回为 0。