繁体   English   中英

MySQL:仅获取非空列

[英]MySQL: get only non-null columns

有表:

在此处输入图片说明

我需要获取带有“列”指定值的非空列。

尝试这个

select * from table where category='1-137-2891' and defence is not null and type is not null

但显然得到空集。

我想我已经为您找到了答案。

假设您有这样的表:

create table t (a int, b int);
insert into t (a, b) values (1, null);

然后,您可以创建动态SQL语句并使用EXECUTE命令执行它;

set @sql = (select concat('select ',
case when sum(a) is null then '' else 'A' end,
case when sum(b) is null then '' else ', ' end, 
case when sum(b) is null then '' else 'B' end,
              ' from t')
from t);

prepare stmt FROM @sql;
execute stmt;
deallocate prepare stmt;

这是一个例子

SELECT * FROM table WHERE('data1'不为null)

暂无
暂无

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

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