繁体   English   中英

将 nulll 值与非 null 值 SQL 进行比较

[英]Compare nulll value with non null value SQL

我有如下两列:

A栏 B栏
A1 NULL
A1 A1
B1 C1。

当我如下查询这些列时:

SELECT A 列,B 列来自表其中 A 列!= B 列

我期待以下结果:

A栏 B栏
A1 NULL
B1。 C1

但是我的查询只给了我第二行作为结果。

SQL 中的 null 等于未知

它也可以有 A1 作为值,因此如果您检查 A1 是否不是 null,它将不会显示

Null 值不参与相等运算,因为没有可比较的值。

您需要在比较语句中包含对 null 的检查。

SELECT Column A, Column B from table where  ISNULL(Column A,0) <> ISNULL(Column B,0)
SELECT * from Table where IFNULL(`Column A`, 'NULL') != IFNULL(`Column B`, 'NULL')

您可以使用以下解决方法

select * from your_table
except distinct
select * from your_table
where columnA = columnB    

与 output

在此处输入图像描述

考虑下面

select *
from your_table
where columnA is distinct from columnB    

与 output

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM