MySQL:只获取非空列

时间:2013-10-23 13:04:49

标签: mysql null

有表格:

enter image description here

我需要获取非空列,其中“column”指定值。

试试这个

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

但显然得到空集。

2 个答案:

答案 0 :(得分:1)

我想我已经找到了答案。

假设你有这样的表:

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;

Here is an example

答案 1 :(得分:-1)

SELECT * FROM表WHERE('data1'不为空)