簡體   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