MYSQL 语法不评估不等于存在 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16186674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MYSQL syntax not evaluating not equal to in presence of NULL
提问by user1904273
I am having trouble with a mysql query. I want to exclude values of 2. So I thought I would do following:
我在使用 mysql 查询时遇到问题。我想排除 2 的值。所以我想我会做以下事情:
table products
id | name | backorder
-------------------
1 | product1 | NULL
2 | product2 | NULL
3 | product3 | 2
SELECT name from `products` p
WHERE backorder <> '2'
However, This is not giving the desired result of product1, product 2 It is giving an empty results table.
但是,这并没有给出 product1 的预期结果,product 2 它给出了一个空的结果表。
On the other hand if I use
另一方面,如果我使用
SELECT name from `products` p
WHERE backorder = '2'
Then it produces: product3
. But I want to get those records where it is not equal to 2
.
然后它产生:product3
。但我想得到那些不等于的记录2
。
Something is not working with the <> '2'
. Could it be that the NULL
values are throwing it off? Can anyone suggest a fix.
某些东西不适用于<> '2'
. 难道是NULL
价值观把它扔掉了?任何人都可以建议修复。
Thanks in advance!
提前致谢!
回答by John Woo
use IS NULL
or IS NOT NULL
to compare NULL
values because they are simply unknown.
使用IS NULL
或IS NOT NULL
来比较NULL
值,因为它们只是未知的。
SELECT name
from products p
WHERE backorder IS NULL OR backorder <> 2
回答by simhumileco
You can use:
您可以使用:
SELECT `name` FROM `products` `p`
WHERE NOT `backorder` <=> '2'
or
或者
SELECT `name` FROM `products` `p`
WHERE !(`backorder` <=> '2')
回答by jbkhere
Sorry to open this
抱歉打开这个
We can use this also
我们也可以使用这个
SELECT name
from products p
WHERE COALESCE(backorder,1) <> 2
回答by sentil kumar
Try this and see.
试试这个,看看。
SELECT name from `products` p
WHERE backorder != '2'