mysql預存程序中遍曆數組字串的兩種方式

來源:互聯網
上載者:User

標籤:

第一種:多次使用substring_index()的方法

  1. DELIMITER $$ 
  2. DROP PROCEDURE IF EXISTS `array`$$ 
  3. CREATE  PROCEDURE `array`() 
  4. BEGIN 
  5. SET @array_content="www mysql com hcymysql blog 51cto com"; 
  6. SET @i=1; 
  7. SET @count=CHAR_LENGTH(@array_content)-CHAR_LENGTH(REPLACE(@array_content,‘ ‘,‘‘)) + 1; 
  8. -- 得出數群組成員總數 
  9. CREATE TABLE test.tmp(field1 VARCHAR(100)); 
  10. WHILE @i <= @count 
  11. DO 
  12. INSERT INTO test.tmp VALUES  
  13. (SUBSTRING_INDEX(SUBSTRING_INDEX(@array_content,‘ ‘,@i),‘ ‘,-1));      
  14. -- 依次插入每個成員  
  15. SET @[email protected]+1; 
  16. END WHILE; 
  17. END$$ 
  18. DELIMITER ;

 

第二種:使用遍曆每個字元的方法

drop procedure if exists Pro_DeleteLog; -- 存在即刪除create procedure Pro_DeleteLog( Qualification varchar(100))  -- 定義參數begin    declare i int ;     declare Start1 int;     declare Length int ;    declare TotalLenght int ;    declare filed varchar(100) ;    declare sqlStr varchar(2000) ;    declare stmtNovelSearch varchar(2000) ;  set i=1;  set Start1=1;  set Length=0;  set TotalLenght=length(Qualification);  -- 計算輸入參數長度      select TotalLenght;   while i <=TotalLenght do -- i=1 開始         -- select SUBSTRING(Qualification,i,1);                if(SUBSTRING(Qualification,i,1)=‘,‘) -- 注意 mysql 的substring函數截取字串是從1開始的,而不是0 與java JavaScript不同。-- 從第i位開始截取一個字元,看是否等於,                then                            set filed=SUBSTRING(Qualification,Start1,Length);-- 從start1開始截取length長度的字元                                                 select filed;                                                            set @sqlStr = CONCAT(‘insert into end_results(accessid,filedName,errinfo)select * from (select w1.accessid ,\‘‘,filed ,‘\‘ as filedName,‘,filed,‘ from wdd_audit w1 ,wdd_dbaccessinfo w2 where w1.accessid=w2.accessid )a where not exists(select e.‘,filed ,‘ from exp_results e where e.‘,filed,‘=a.‘,filed,‘ or(e.‘,filed,‘ is null and a.‘,filed,‘ is null) );‘);                        -- 拼裝sql語句select @sqlStr;                            PREPARE stmtNovelSearch FROM @sqlStr; -- prepare 必須採用這種傳參方式                        execute stmtNovelSearch;                                deallocate prepare stmtNovelSearch;                        set Start1=i+1;                         set Length=0;                else                    set Length=Length+1; -- 如果不是, 則截取的長度加1                                     end if;            set i=i+1;         end while;end -- 調用預存程序  欄位名以,分隔  注意最後 , 結尾call Pro_DeleteLog(‘payload,backresult,dataset,effectrow,clientuser,loginuser,clientprg,‘) 個人推薦第一種;

mysql預存程序中遍曆數組字串的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.