日期:2014-05-16  浏览次数:20813 次

Mysql 之 条件判断、循环语句
官方英文文档:
http://dev.mysql.com/doc/refman/5.0/en/flow-control-statements.html

**************************    IF    **************************
语法:
IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF


实例:
DELIMITER$$

DROP PROCEDURE IF EXISTS pro_if$$
CREATE PROCEDURE pro_if(
	i INT
)
BEGIN
	IF i < 60 THEN
		SELECT '需继续努力,争取过线!' AS result;
	ELSEIF i < 85 THEN
		SELECT '再努力点,争取达优!' AS result;
	ELSE 
		SELECT '不错哦,恭喜,恭喜!' AS result;
	END IF;
END$$



**************************    CASE    **************************
语法:
CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE


Or:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE


实例:
DELIMITER$$

DROP PROCEDURE IF EXISTS pro_case$$
CREATE PROCEDURE pro_case(
	i INT
)
BEGIN
	CASE i
	WHEN 1 THEN SELECT 'you male!' AS result;
	WHEN 0 THEN SELECT 'you female!' AS result;
	ELSE
		SELECT 'third sex!' AS result;
	END CASE;
END$$