mysql求2个或以上字段为NULL值的示例分析

41次阅读
没有评论

共计 2135 个字符,预计需要花费 6 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章主要为大家展示了“mysql 求 2 个或以上字段为 NULL 值的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“mysql 求 2 个或以上字段为 NULL 值的示例分析”这篇文章吧。

核心代码

/*--------------------------------
求 2 个或以上字段为 NULL  的记录
id, id1, id2, id3, id4, id5, id6
在 t1  表中有个字段;其中 id 是主键;怎样打印其中个字段或以上为 NULL  的记录 id?另外,存储过程中怎么实现按顺序一条一条读取记录最方便?注:主键 id  是没有顺序的,也可能是字符串的;-----------------------------------------*/
drop table if exists t1;
create table t1(id int,id1 int,id2 int,id3 int,id4 int,id5 int,id6 int);
insert t1 select
1,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) then
select * from t1 where id=idd;
end if ;
until 0 end repeat;
close cur;
end ;
delimiter ;
+------+------+------+------+------+------+------+
| id | id1 | id2 | id3 | id4 | id5 | id6 |
+------+------+------+------+------+------+------+
| 1 | 1 | 1 | 1 | 1 | NULL | NULL |
+------+------+------+------+------+------+------+
1 row in set (0.10 sec)
+------+------+------+------+------+------+------+
| id | id1 | id2 | id3 | id4 | id5 | id6 |
+------+------+------+------+------+------+------+
| 2 | NULL | NULL | NULL | 1 | 2 | 3 |
+------+------+------+------+------+------+------+
1 row in set (0.14 sec)
+------+------+------+------+------+------+------+
| id | id1 | id2 | id3 | id4 | id5 | id6 |
+------+------+------+------+------+------+------+
| 5 | NULL | 3 | 4 | NULL | NULL | NULL |
+------+------+------+------+------+------+------+
1 row in set (0.17 sec)
*/

以上是“mysql 求 2 个或以上字段为 NULL 值的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计2135字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)