mysql select * from table where not null

时间:2017-06-06 08:50:02

标签: mysql

是否可以从表中选择* 哪里不为空?

基本上我想要一个表中的所有结果,但是如果一列有一个行的空值我不想在我的输出文件中看到null是一个csv文件?

1 个答案:

答案 0 :(得分:2)

如果group_concat有一堆列,而且这些列中的任何一列都包含null,那么结果为null。因此,您可以使用此特性与从information_schema列中获取的列名一起构建预准备语句。

drop table if exists t;
create table t (tID int, Name varchar(20),   Type varchar(20),   Start_Date date,  End_Date date);
insert into t values
(1,   null , 'Retail' , '2010-01-01', '2010-07-21'),
(1,   'Cust_1' , null   , '2010-07-22', '2012-05-17'),
(1,   'Cust_1' , 'Corp'   , '2012-05-18', '2012-12-31');



select group_concat(column_name) into @gc from information_schema.columns where table_name = 't' and table_schema = 'sandbox';
set @sqlstmt = concat('select * from t where concat(', @gc, ') is not null;');

#select @sqlstmt;

prepare sqlstmt from @sqlstmt;
execute sqlstmt;
deallocate prepare sqlstmt;

结果

+------+--------+------+------------+------------+
| tID  | Name   | Type | Start_Date | End_Date   |
+------+--------+------+------------+------------+
|    1 | Cust_1 | Corp | 2012-05-18 | 2012-12-31 |
+------+--------+------+------------+------------+
1 row in set (0.00 sec)