• MySql中循环的使用


    一、while循环 语法:WHILE [条件] DO [逻辑] END WHILE;

    delimiter $$
    DROP FUNCTION IF EXISTS `fn_findCharCount` $$
    CREATE FUNCTION fn_findCharCount
    (in_string VARCHAR(500),
    in_find_str VARCHAR(50)
    )
    RETURNS INT
    BEGIN
    
        DECLARE tcount INT DEFAULT 0;
        DECLARE new_str VARCHAR(500);
        DECLARE scount INT;
    
        SET new_str = in_string;
    
        SELECT INSTR(new_str,in_find_str) INTO scount;
    
    WHILE scount>0 DO
    SET tcount = tcount+1;
    SELECT SUBSTRING(new_str FROM (scount+1) FOR CHAR_LENGTH(new_str)) INTO new_str;
    SELECT INSTR(new_str,in_find_str) INTO scount;
    END WHILE;
        
        RETURN(tcount);
    
    END$$
    delimiter ;

    这是一个mysql自定义函数,里面用了while循环;这个函数是用来判断一个字符串在另一个字符串中出现的次数。

    二、 repeat循环:repeat [逻辑] until [条件判断] end repeat; 注意:until判断末尾不要加分号 ; 

    delimiter $$
    create procedure _repeat()
    begin
    declare a int default 10;
    repeat
    set a=a-1;
    until a<5
    end repeat;
    select a;
    end $$
    delimiter ;

    三、loop循环:注意 loop 一般要和一个标签一起使用,且在 loop 循环中一定要有一个判断条件,能够满足在一定的条件下跳出 loop 循环(即 leave )!

    delimiter $$
    create procedure test_loop()
    begin
    declare t int default 0;
    label:loop
    set t=t+1;
    if t>10 then leave label;
    end if;
    end loop label;
    select t;
    end $$
    delimiter ;
  • 相关阅读:
    【R】如何去掉数据框中包含非数值的行?
    解读NoSQL数据库的四大家族
    MapReduce
    从网站上扒网页,保存为file文件格式
    jfinal 模板引擎
    pycharm的版本对应问题
    AttributeError: module 'DBBase' has no attribute 'DBBase'
    四则运算 python
    用命令行去运行程序
    Pandas入门CNV.TXT数据分析
  • 原文地址:https://www.cnblogs.com/xsj1989/p/7127020.html
Copyright © 2020-2023  润新知