2017-06-06 120 views
-2

是否可以从表中选择*其中不为空?mysql select * from table where not null

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

+0

尝试哪里是什么NOT NULL –

+0

有数百行的,所以我不希望有,因为,那里ROW1不是null,2行不为空,等我想知道如果你可以做任何表中的行不为空 –

+0

“WHAT”应该是不允许为空的列。因此,例如,如果您不希望列2在您的导出中为空,那么您会说“WHERE column_2 IS NOT NULL” – FMashiro

回答

2

如果您group_concat一堆列,并且其中任何一列包含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) 
相关问题