mysql 流程函数

CASE语句

select stu_id,grade,
case when grade > 80 then 'good'
         when grade between 60 and 80 then 'not bad'
         else 'bad' end as grade_level
from stu_grade;

IF语句

select grade, if(grade>= 60,'pass','fail') as grade_judge from stu_grade;

IF age>20 THEN SET @count1=@count1+1;  
ELSEIF age=20 THEN SET @count2=@count2+1;  
ELSE SET @count3=@count3+1;  
END IF;

IFNULL

select stu_id,grade, ifnull(grade,0) as new_grade from stu_grade;

 ASCII(str)

自定义函数

select ascii('abc');
mysql> CREATE FUNCTION   test.formatDate(fdate datetime)
    -> RETURNS VARCHAR(255)
    -> BEGIN
    -> DECLARE x VARCHAR(255) DEFAULT '';
    -> SET x= date_format(fdate,'%Y年%m月%d日%h时%i分%s秒');
    -> RETURN x;
    -> END $$
Query OK, 0 rows affected (0.11 sec)

mysql> DELIMITER ;
mysql> SELECT   formatDate(now());

WHILE @count<100 DO 
SET @count=@count+1; 
END WHILE ; 

WHILE @count<100 DO
SET @count=@count+1;
END WHILE;

REPEAT 
SET @count=@count+1; 
UNTIL @count=100 
END REPEAT ; 

REPEAT
SET @count=@count+1;
UTILL @count=100
END REPEAT;

add_num: LOOP 
SET @count=@count+1; 
IF @count=100 THEN 
LEAVE add_num ; 
ELSE IF MOD(@count,3)=0 THEN 
ITERATE add_num; 
SELECT * FROM employee ; 
END LOOP add_num ; 



posted @ 2018-08-06 10:02  对数螺旋线  阅读(143)  评论(0编辑  收藏  举报