在MySQL中的多列中测试NULL [英] Test for NULLs in multiple columns in MySQL

查看:98
本文介绍了在MySQL中的多列中测试NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从表中的列值为NULL的列中选择列.

I want to select columns from a table where the values of columns are NULL.

例如:

SELECT * FROM table1 WHERE column1 IS NULL;

上面的查询返回column1没有任何值的行.

the above query returns rows where column1 doesn't have any value.

但是我想返回多个列(其中column1,column2,....)

But I want to return multiple columns(where column1,column2,....)

我想在MYSQL中使用它

I want to use this in MYSQL

推荐答案

SELECT *
FROM table1
WHERE coalesce(column1, column2, column3) IS NULL;

您将必须枚举所有必填列. (我应该承认这是一种黑客行为,不应在生产代码中使用)

You will have to enumerate all required columns. (I should confess this is a hack and should not be used in production code)

UPD

如果要检查至少一列是否为空,则应使用OR:

IF you want to check if at least a single column is null you should use OR:

SELECT *
FROM table1
WHERE column1 IS NULL or column2 IS NULL;

这篇关于在MySQL中的多列中测试NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆