mysql 求解求2个或以上字段为NULL的记录

时间:2021-05-24

核心代码

drop table if exists t1;create table t1(id int,id1 int,id2 int,id3 int,id4 int,id5 int,id6 int);insert t1 select1,1,1,1,1,null,null union all select 2,null,null,null,1,2,3 union all select 3,1,2,3,4,5,6 union all select 4,1,2,3,4,5,null union all select 5,null,3,4,null,null,null ;delimiter $$create procedure usp_c_null()begin declare n_c int;declare idd int;declare cur cursor for select id,case char_length(concat(ifnull(id1,'@'),ifnull(id2,'@'),ifnull(id3,'@'),ifnull(id4,'@'),ifnull(id5,'@'),ifnull(id6,'@')))-char_length(replace(concat(ifnull(id1,'@'),ifnull(id2,'@'),ifnull(id3,'@'),ifnull(id4,'@'),ifnull(id5,'@'),ifnull(id6,'@')),'@','') ) when 6 then 6 when 5 then 5 when 4 then 4 when 3 then 3 when 2 then 2 when 1 then 1 else 0 end as c from t1;declare exit HANDLER for not found close cur ;open cur;repeat fetch cur into idd,n_c;if(n_c>=2) thenselect * from t1 where id=idd;end if ;until 0 end repeat;close cur;end ;$$delimiter ;

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章