mysql 求解求2個(gè)或以上字段為NULL的記錄
核心代碼
/*-------------------------------- 求2個(gè)或以上字段為NULL 的記錄 t1: id, id1, id2, id3, id4, id5, id6 在t1 表中有個(gè)字段; 其中id是主鍵; 怎樣打印其中個(gè)字段或以上為NULL 的記錄id? 另外,存儲(chǔ)過(guò)程中怎么實(shí)現(xiàn)按順序一條一條讀取記錄最方便? 注:主鍵id 是沒(méi)有順序的,也可能是字符串的; -----------------------------------------*/ 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) */
相關(guān)文章
Mysql Binlog快速遍歷搜索記錄及binlog數(shù)據(jù)查看的方法
這篇文章主要介紹了Mysql Binlog快速遍歷搜索記錄及binlog數(shù)據(jù)查看的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01
MySQL:Unsafe statement written to the binary log using state
這篇文章主要介紹了MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM,需要的朋友可以參考下2016-05-05
mysql如何動(dòng)態(tài)創(chuàng)建連續(xù)時(shí)間段
這篇文章主要介紹了mysql如何動(dòng)態(tài)創(chuàng)建連續(xù)時(shí)間段問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
MySQL Community Server壓縮包安裝配置方法
這篇文章主要為大家詳細(xì)介紹了MySQL Community Server壓縮包安裝配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
MySQL FIND_IN_SET 函數(shù)實(shí)戰(zhàn)實(shí)例
FIND_IN_SET是MySQL處理分隔字符串的函數(shù),用于判斷字符串是否存在于列表中,返回位置或0,適用于標(biāo)簽、分類(lèi)等多值場(chǎng)景,但無(wú)法使用索引,性能較差,建議結(jié)合替代方案或優(yōu)化設(shè)計(jì),注意大小寫(xiě)敏感和空值處理問(wèn)題,對(duì)mysql find_in_set函數(shù)感興趣的朋友一起看看吧2025-07-07
MySQL從零開(kāi)始了解數(shù)據(jù)庫(kù)開(kāi)發(fā)之表的操作解析
這篇文章介紹了MySQL中數(shù)據(jù)表的基本操作,包括創(chuàng)建、修改和刪除表的相關(guān)語(yǔ)法和注意事項(xiàng),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-09-09
MySQL中UPDATE與DELETE語(yǔ)句的使用教程
這篇文章主要介紹了MySQL中UPDATE與DELETE語(yǔ)句的使用教程,是MySQL入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-12-12
MySQL創(chuàng)建數(shù)據(jù)庫(kù)和創(chuàng)建數(shù)據(jù)表
MySQL?是最常用的數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)操作中,基本都是增刪改查操作,簡(jiǎn)稱(chēng)CRUD。但是,這篇文章主要介紹了數(shù)據(jù)庫(kù)和數(shù)據(jù)表如何創(chuàng)建,想詳細(xì)了解的小伙伴可以參考閱讀一下2023-03-03

