MYSQL语法不评估不等于存在NULL

MYSQL语法不评估不等于存在NULL

本文介绍了MYSQL语法不评估不等于存在NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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 NULLIS 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

08-15 17:17