知识点四:MySQL流程控制语句(7-10)
选择语句:
(IF ELSE ELSE IF CASE 分支)IFNULL函数
IF语法:
语法规则:
IF search_condition THEN statmen_list ;
[SLSEIF search_condition THEN statmen_list];
ELSE statmen_list;
END IF;
1 --7 流程控制语句之选择语句 2 DELIMITER // 3 CREATE PROCEDURE p_test7(IN age INT) 4 BEGIN 5 IF age >=18 THEN SELECT '成年人'; 6 ELSE SELECT '未成年人'; 7 END IF; 8 END 9 // 10 11 DELIMITER ; 12 SET @age =4; 13 CALL p_test7(@age); 14 15 SELECT @age; 16 17 18 DELIMITER // 19 CREATE PROCEDURE p_test8(IN age INT) 20 BEGIN 21 IF age >=18 AND age < 60 THEN SELECT '成年人'; 22 ELSEIF age >= 60 THEN SELECT '老年人'; 23 ELSE SELECT '未成年人'; 24 END IF; 25 END 26 // 27 28 DELIMITER ; 29 SET @age =60; 30 CALL p_test8(@age); 31 32 SELECT @age;
CASE语法
CASE语句也用来进行条件判断,其可以实现比IF语句更复杂的条件判断。
CASE语法的基本形势如下:
CASE case_value
WHEN when_value THEN statement_list;
[WHEN when_value THEN statement_list];…;
[ELSE statement_list];
END CASE;
1 --8 流程控制语句之选择语句CASE分支 2 3 --默认数据(创建工资表) 4 CREATE DATABASE mzjj; 5 USE mzjj; 6 7 CREATE TABLE salaries( 8 emp_no INT KEY NOT NULL AUTO_INCREMENT, 9 salary INT, 10 from_data DATE DEFAULT '2015-04-01', 11 to_data DATE DEFAULT '2015-05-01' 12 )AUTO_INCREMENT=1001; 13 14 15 16 17 DELIMITER // 18 CREATE PROCEDURE p_addsalary(IN v_empno INT) 19 BEGIN 20 DECLARE addS int; 21 CASE v_empno 22 WHEN 1001 THEN SET addS = 1500; 23 WHEN 1002 THEN SET addS = 2500; 24 WHEN 1003 THEN SET addS = 3500; 25 ELSE SET adds =1000; 26 END CASE; 27 UPDATE salaries SET salary = addS WHERE emp_no= v_empno; 28 END 29 // 30 31 DELIMITER ; 32 SET @v_empno =1001; 33 CALL p_addsalary(@v_empno); 34 35 SELECT @v_empno; 36 37 --默认数据(创建员工信息表) 38 CREATE TABLE employees( 39 emp_no INT KEY NOT NULL AUTO_INCREMENT, 40 birth_data DATE DEFAULT '2015-04-01', 41 first_name VARCHAR(20), 42 last_name VARCHAR(20), 43 gender ENUM('M','F'), 44 hire_data DATE DEFAULT '2015-05-01' 45 )AUTO_INCREMENT=1001; 46 47 SELECT emp_no,first_name,last_name, 48 (CASE gender WHEN 'M' THEN '男'ELSE '女' END) AS '性别' FROM employees;
IFNULL函数:
用法:
IFNULL(exp1,exp2)
如果exp1是NULL则返回exp2;否则返回exp1;
示例:
SELECT IFNULL(null,'不是空');
SELECT IFNULL(123,'不是空');
1 --测试IFNULL函数 2 UPDATE employees SET first_name=NULL WHERE emp_no =1001; 3 SELECT IFNULL(first_name,'无名字') FROM employees;
循环语句:
WHILE语句:
语法规则:
WHILE 条件 DO
内容
END WHILE;
1 --9 流程控制(WHILE循环语句) 2 --计算1加到100 3 DELIMITER // 4 CREATE PROCEDURE p_addnum() 5 BEGIN 6 DECLARE i int DEFAULT 1; 7 DECLARE addresult int DEFAULT 0; 8 WHILE i<= 100 DO 9 SET addresult = addresult +i; 10 SET i = i+1; 11 END WHILE; 12 SELECT addresult; 13 END 14 // 15 16 DELIMITER ; 17 CALL p_addnum(); 18 19 --向表中加入1000条数据 20 DELIMITER // 21 CREATE PROCEDURE p_insertEmp() 22 BEGIN 23 DECLARE maxempno int DEFAULT 0; 24 DECLARE i int DEFAULT 1; 25 WHILE i<= 1000 DO 26 SELECT MAX(emp_no) into maxempno FROM employees; 27 SET maxempno = maxempno+1; 28 INSERT INTO employees(emp_no,birth_data, first_name,last_name, gender,hire_data) 29 VALUES(maxempno,'1990-1-1','jim','ju','M','2000-1-1'); 30 SET i =i+1; 31 END WHILE; 32 END 33 // 34 35 DELIMITER ; 36 CALL p_insertEmp();
1 --任务作业 2 --更新编号为偶数的gender字段为M 3 4 DELIMITER // 5 CREATE PROCEDURE changeEmp() 6 BEGIN 7 8 DECLARE maxempno int DEFAULT 0; 9 DECLARE sex VARCHAR(2) DEFAULT 'M'; 10 DECLARE minempno int DEFAULT 1; 11 SELECT MAX(emp_no) into maxempno FROM employees; 12 SELECT MIN(emp_no) into minempno FROM employees; 13 WHILE minempno <= maxempno DO 14 CASE minempno%2 15 WHEN 0 THEN SET sex = 'M'; 16 ELSE SET sex = 'M'; 17 END CASE; 18 UPDATE employees SET gender = sex WHERE emp_no= minempno; 19 SET minempno =minempno+1 ; 20 END WHILE; 21 END 22 // 23 DELIMITER ; 24 CALL changeEmp(); 25 26 SELECT * FROM employees; 27 28 DELIMITER // 29 CREATE PROCEDURE changeEmp2() 30 BEGIN 31 32 DECLARE maxempno int DEFAULT 0; 33 DECLARE minempno int DEFAULT 1; 34 SELECT MAX(emp_no) into maxempno FROM employees; 35 SELECT MIN(emp_no) into minempno FROM employees; 36 WHILE minempno <= maxempno DO 37 IF minempno%2=0 THEN UPDATE employees SET gender = 'F' WHERE emp_no= minempno; 38 END IF; 39 SET minempno =minempno+1 ; 40 END WHILE; 41 END 42 // 43 DELIMITER ; 44 CALL changeEmp2();
REPEAT语句:
语法规则:
REPEAT
内容
UNTIL 条件 //推出循环的条件
END REPEAT;
1 --rapeat测试 2 DELIMITER // 3 CREATE PROCEDURE changeEmp1() 4 BEGIN 5 6 DECLARE maxempno int DEFAULT 0; 7 DECLARE sex VARCHAR(2) DEFAULT 'M'; 8 DECLARE minempno int DEFAULT 1; 9 SELECT MAX(emp_no) into maxempno FROM employees; 10 SELECT MIN(emp_no) into minempno FROM employees; 11 REPEAT 12 if minempno%2 =0 then update employees SET gender='F' WHERE emp_no = minempno; 13 END IF; 14 SET minempno = minempno+1; 15 UNTIL minempno>maxempno 16 END REPEAT; 17 END 18 // 19 DELIMITER ; 20 CALL changeEmp1();
LOOP语句:
语法规则:
LOOP名字:LOOP
内容
IF 条件 THEN
LEAVE LOOP名字;
END IF;
END LOOP;
1 --loop测试 2 DELIMITER // 3 CREATE PROCEDURE changeEmp3() 4 5 BEGIN 6 DECLARE maxempno int DEFAULT 0; 7 DECLARE minempno int DEFAULT 1; 8 SELECT MAX(emp_no) into maxempno FROM employees; 9 SELECT MIN(emp_no) into minempno FROM employees; 10 11 myloop:loop 12 if minempno%2 =1 then update employees SET hire_data='1900-12-11' WHERE emp_no = minempno; 13 END IF; 14 SET minempno = minempno+1; 15 IF minempno>maxempno THEN LEAVE myloop; 16 END IF; 17 END loop; 18 END 19 // 20 21 DELIMITER ; 22 CALL changeEmp3();