字段可为null时一些注意事项

情景:某个字段可为Null,并且数据库中已经有一些数据了,其中该字段有为Null,有不为null的,测试下列sql语句的正确性。

测试数据库Mysql:

//找 Position字段不为Null的数据

SELECT * FROM `doctor` where Position <> null   无数据  错误示例

SELECT * FROM `doctor` where Position != null    无数据  错误示例

SELECT * FROM `doctor` where Position is not null    正确示例

//找 Position字段为null的数据

SELECT * FROM `doctor` where Position =null   错误示例

SELECT * FROM `doctor` where Position is null  正确示例

可知:对Null的判断只能用 is 和 is not

当用 <> 时:

select * from  docter where position <> 1   查出来的数据 position不为1且不为null

用 != 也测试了,效果一样。听说 这两个不等于有一个会查出 null,这里证明了都查不出null,有人遇到过吗?

猜你喜欢

转载自blog.csdn.net/qq_38261174/article/details/100522975