我有一个包含7个字段的表,我想在任意3列中选择具有空值的记录
答案 0 :(得分:2)
好吧,因为其他人在编写查询时遇到了这么难。在这里:
select *
from mytable
where (col1 is null) + (col2 is null) + (col3 is null) +
(col4 is null) + (col5 is null) + (col6 is null) + (col7 is null) = 3;
这在MySQL中使用了true = 1和false = 0。无论如何,我在这里做的只是计算空值。
答案 1 :(得分:1)
select *
from tableName
having sum((case when column1 is null than 1 end)+
(case when column2 is null than 1 end)+
(case when column3 is null than 1 end)+
(case when column4 is null than 1 end)+
(case when column5 is null than 1 end)+
(case when column6 is null than 1 end)+
(case when column7 is null than 1 end)) >= 3
答案 2 :(得分:0)
创建表脚本
Create Table SevenColumnTable
(
Col1 varchar(10),
Col2 varchar(10),
Col3 varchar(10),
Col4 varchar(10),
Col5 varchar(10),
Col6 varchar(10),
Col7 varchar(10)
);
示例插入
Insert into SevenColumnTable(Col1,Col2,Col3,Col4)
Select 'A','B','C','D';
Insert into SevenColumnTable(Col5,Col2,Col3,Col4)
Select 'A','B','C','D';
Insert into SevenColumnTable(Col2,Col3,Col4)
Select 'A','B','C';
Insert into SevenColumnTable(Col5,Col2,Col3)
Select 'A','B','C';
选择声明
SELECT * FROM SevenColumnTable where
ifnull(Col1,1)+ifnull(Col2,1)+ifnull(Col3,1)+ifnull(Col4,1)+ifnull(Col5,1)+ifnull(Col6,1)
+ifnull(Col6,1)=3;