开发者

MySQL - results from 'SELECT WHERE STH' and 'SELECT WHERE NOT STH' don't sum to full table

开发者 https://www.devze.com 2023-02-01 13:58 出处:网络
How is that possible that result开发者_StackOverflow中文版s from those 2 queries: SELECT * FROM `workers` WHERE `name` = \'Smith`

How is that possible that result开发者_StackOverflow中文版s from those 2 queries:

SELECT * FROM `workers` WHERE `name` = 'Smith`

and

SELECT * FROM `workers` WHERE NOT `name` = 'Smith`

doesn't sum to whole table workers?


Because NULL in name field does not get into either query.

In ternary logic which SQL uses, NULL = 'Smith' and NOT NULL = 'Smith' both evaluate to NULL and are filtered out.

Use NULL-safe comparison operator, <=>:

SELECT * FROM `workers` WHERE `name` <=> 'Smith`

and

SELECT * FROM `workers` WHERE NOT `name` <=> 'Smith`
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号