mysql 求解求2个或以上字段为NULL的记录
核心代码
/*-------------------------------- 求2个或以上字段为NULL 的记录 t1: 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) */
原文链接:http://blog.csdn.net/feixianxxx/article/details/5802668
(资源库 www.zyku.net)
您可能感兴趣的文章
- 04-05mysql 转换NULL数据方法(必看)
- 02-20Zend Studio for Eclipse的java.lang.NullPointerExce
- 12-25php中empty(), is_null(), isset()函数区别
- 01-12安可好生活-安可好生活应用软件功能介
- 01-11军队文职准题库-军队文职准题库应用软
- 09-22荣耀magic3至臻版怎么设置支付宝应用
- 09-19微博绿藤市同城热搜在哪
- 07-21尊云
- 02-28opporeno6添加指纹解锁教程
- 12-03菜鸟裹裹怎么开启长辈模式
- 03-31微信小程序后台登录(非微信账号登录)实
- 01-12放心连WiFi-放心连WiFi应用软件功能介
- 01-11悦盈优聘-悦盈优聘应用软件功能介绍
- 05-312021年阿里云年中大促活动特惠套餐优
- 11-18扫描全能王去除水印教程分享
- 04-27华为nova8pro关闭Volte高清通话功能方
- 11-01华为matebook13和13s配置对比一览
- 01-12和家相册-和家相册应用软件功能介绍
- 10-212021淘宝双十一组队赢红包怎么退队
- 01-11遇见她-遇见她应用软件功能介绍
最近更新
阅读排行
猜你喜欢
- 10-08哔哩哔哩怎样添加同城
- 11-21ipad开启录屏麦克风教程介绍
- 01-30小米air2pro降噪功能启用教程
- 02-10Pytorch GPU内存占用很高,但是利用率
- 11-26oppo手机怎么关闭消息播报提醒
- 03-19荣耀60开启桌面一键锁屏方法
- 02-23DedeCMS内容页调用当前栏目名称及链接
- 08-14opporeno6应用自启动设置教程分享
- 09-14Tableau是什么?Tableau介绍
- 04-27荣耀60se设置返回键教程