MySQL tinyint比较器返回空集

时间:2014-01-06 21:45:27

标签: php mysql null tinyint

我正在执行一个select语句,试图查找列inactive未设置为1的数据,但结果始终为空集。为什么会这样?

 mysql> select id, time, num, inactive from data limit 10;
+--------+------------+------+----------+
| id     | time       | num  | inactive |
+--------+------------+------+----------+
| 276975 | 1388024838 |   55 |     NULL |
| 276976 | 1388025072 |  138 |     NULL |
| 276977 | 1388025435 |  211 |     NULL |
| 276978 | 1388025841 |  240 |     NULL |
| 276979 | 1388026372 |  329 |     NULL |
| 276980 | 1388026515 |  119 |     NULL |
| 276981 | 1388027029 |   57 |     NULL |
| 276982 | 1388027117 |  314 |     NULL |
| 276983 | 1388027251 |   47 |     NULL |
| 276984 | 1388027340 |   68 |     NULL |
+--------+------------+------+----------+
10 rows in set (0.00 sec)

所以我希望这会起作用,但事实并非如此:

mysql> select id from data where inactive != 1;
Empty set (0.01 sec)

以下是更多信息:

mysql> describe data;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| id          | bigint(20)    | NO   |     | NULL    |       |
| time        | bigint(20)    | YES  |     | NULL    |       |
| num         | int(11)       | YES  |     | NULL    |       |
| inactive    | tinyint(1)    | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+

2 个答案:

答案 0 :(得分:2)

NULL不等于任何东西。您需要明确接受空值:

select id from data where inactive <>1 or inactive is null;

有关NULL处理的详情,请参阅Working with NULL

答案 1 :(得分:1)

这是因为SQL使用三个有价值的逻辑。 inactive != 1不包含NULL

select id from data where inactive != 1 OR inactive IS NULL;