开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜