2

I am doing a select statement trying to look for data where the column inactive is not set to 1, but the result is always Empty Set. Why is this happening?

 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)

So I would expect this to work, but it doesn't:

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

Here's some more info:

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    |       |
+-------------+---------------+------+-----+---------+-------+
4

2 回答 2

2

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

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

有关处理的更多信息,请参阅使用 NULLNULL

于 2014-01-06T21:48:12.497 回答
1

这是因为 SQL 使用三值逻辑。inactive != 1不包括NULL

select id from data where inactive != 1 OR inactive IS NULL;
于 2014-01-06T21:47:56.820 回答