2017-06-06 174 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) 
相關問題