简体   繁体   English

Mysql选择行两列不具有相同值

[英]Mysql Select Rows Where two columns do not have the same value

I'm trying to run a query where two columns are not the same, but it's not returning any results: 我正在尝试运行一个查询,其中两列不相同,但它没有返回任何结果:

SELECT * FROM `my_table` WHERE `column_a` != `column_b`;

column_a AND column_b are of integer type and can contain nulls. column_a AND column_b是整数类型,可以包含空值。 I've tried using <> IS NOT, etc without any luck. 我尝试过使用<> IS NOT等没有运气。 It's easy to find if they're the same using <=>, but <> and != doesn't return any rows. 使用<=>很容易找到它们是否相同,但<>和!=不会返回任何行。 (using Mysql 5.0). (使用Mysql 5.0)。

Thoughts? 思考?

The problem is that a != b is NULL when either a or b is NULL. 问题是当a或b为NULL时,a!= b为NULL。

<=> is the NULL-safe equals operator. <=>是NULL安全等于运算符。 To get a NULL-safe not equal to you can simply invert the result: 要获得不等于NULL的安全性,可以简单地反转结果:

SELECT *
FROM my_table
WHERE NOT column_a <=> column_b

Without using the null safe operator you would have to do this: 如果不使用null safe运算符,则必须执行以下操作:

SELECT *
FROM my_table
WHERE column_a != column_b
OR (column_a IS NULL AND column_b IS NOT NULL)
OR (column_b IS NULL AND column_a IS NOT NULL)

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

相关问题 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 选择记录,其中所有行在同一行的两列中具有相同的值 - Select records where all rows have same value in two columns in a single row 如何编辑此mySQL查询以不选择2列中具有相同值的行? - How to edit this mySQL query to not select rows that have same value in 2 columns? 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? SQL - 选择在两列中具有相同值的行 - SQL - select rows that have the same value in two columns 选择两行具有相同列值的位置并回显它们 - Select where two rows have the same column value and echo them mysql选择两列中具有相同值的所有行 - mysql select all rows with same value in two columns 如何选择两列不等于值的行? - How can select rows where two columns do not equal a value? 如何从两个表中选择两个在同一个字段中具有相同值的行? - How to select rows from two tables where both have the same value in the same field? 选择两列不能同时为0的行 - Select rows where two columns cannot be 0 at same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM