问题描述
我在使用mysql查询时遇到问题.我想排除2的值.所以我想我会做以下事情:
I am having trouble with a mysql query. I want to exclude values of 2. So I thought I would do following:
table products
id | name | backorder
-------------------
1 | product1 | NULL
2 | product2 | NULL
3 | product3 | 2
SELECT name from `products` p
WHERE backorder <> '2'
但是,这没有给出product1,product 2的预期结果.
However, This is not giving the desired result of product1, product 2 It is giving an empty results table.
另一方面,如果我使用
SELECT name from `products` p
WHERE backorder = '2'
然后产生:product3
.但是我想获取不等于2
的那些记录.
Then it produces: product3
. But I want to get those records where it is not equal to 2
.
<> '2'
无法正常工作.可能是NULL
值将其扔掉了吗?任何人都可以提出修复建议.
Something is not working with the <> '2'
. Could it be that the NULL
values are throwing it off? Can anyone suggest a fix.
提前谢谢!
推荐答案
使用IS NULL
或IS NOT NULL
来比较NULL
值,因为它们只是未知的.
use IS NULL
or IS NOT NULL
to compare NULL
values because they are simply unknown.
SELECT name
from products p
WHERE backorder IS NULL OR backorder <> 2
- SQLFiddle演示
-
- SQLFiddle Demo
SQLFiddle Demo (added some records)
这篇关于MYSQL语法不评估不等于存在NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!