MySQL - where 条件为 small int

MySQL - where condition with small int

我有状态为 smallint(5) 类型的任务 table。它有 0 或 1 或 2 个值。

当我运行这个查询时,它给我结果

 SELECT * FROM `tasks` WHERE  `status` = 'EMPTY' 

状态中没有EMPTY值,那我怎么得到结果呢?

如果值只能是 012 那么这应该为所有行提供 0 状态:

SELECT *
FROM tasks
WHERE status = 0 

如果有 NULL 个值,那么您可以通过将 WHERE 更改为

来获取这些值
WHERE status IS NULL

NULL0 不同,因此要同时获得两者,您需要执行以下操作:

WHERE status IS NULL OR status = 0

例如,正在发生隐式数据转换

select cast('empty' as int); 

+----------------------+
| cast('empty' as int) |
+----------------------+
|                    0 |
+----------------------+
1 row in set, 1 warning (0.00 sec)

恐怕这取决于你确保你比较同类。