DELIMITER // (设置结束符 其实我也不太明白为啥要这样 记住就行把) CREATE FUNCTION ym_date(mydate DATE) (创建函数 函数名字(参数)) RETURNS VARCHAR(20) (指定函数的返回类型) BEGIN (函数开始,如果函数只有一句可以省略bigin和end) RETURN DATE_FORMAT(mydate,%Y-%m); (函数体) END// (函数结束) DELIMITER ; (把结束符再变成;) CREATE FUNCTION show_name_store(bid INT) RETURNS VARCHAR(20) RETURN (SELECT CONCAT_WS(‘-----‘,book_name,store) FROM bookinfo WHERE book_id=bid); (into的介绍) DELIMITER // CREATE FUNCTION show_name_store(bid INT) RETURNS VARCHAR(20) BEGIN DECLARE result VARCHAR(20) (声明变量) SELECT CONCAT_WS(‘-----‘,book_name,store) INTO result FROM bookinfo WHERE book_id=bid;(对变量result进行赋值。emmm没啥说的) RETURN result; END// DELIMITER ; (删除函数) DROP FUNCTION xxxxx; (又是if else) DELIMITER // CREATE FUNCTION show_level(cid VARCHAR(20)) RETURNS VARCHAR(10) BEGIN DECLARE lev VARCHAR(20); DECLARE money DECIMAL(7,3); SELECT balance INTO money FROM readerinfo WHERE card_id=cid; IF money>=500 THEN SET lev=‘高级‘; ELSEIF money>=300 THEN SET lev=‘普通‘; ELSE SET lev=‘非会员‘; END IF; RETURN lev; END// DELIMITER ; (case的样例) DELIMITER // CREATE FUNCTION show_lev2(cid CHAR(18)) RETURNS barchar(10) BEGIN DECLARE lev VARCHAR(10); DECLARE money DECIMAL(7,3); DECLARE num INT; SELECT balance INTO money FROM readerinfo WHERE card_id=cid; SET num=TRUNCATE(money/100,0); CASE num WHEN 0 THEN SET lev=‘非会员‘; WHEN 1 THEN SET lev=‘普通‘; WHEN 2 THEN SET lev=‘高级‘; ELSE SET lev=‘金牌‘; END CASE; RETURN lev; END// DELIMITER ; (CASE 也可以这样用) CASE WHEN money>500 THEN SET lev=‘高级‘; WHEN money>=300 THEN SET lev=‘普通‘; ELSE SET lev=‘不是‘; END CASE; (以下是循环的样例) DELIMITER // CREATE FUNCTION test(n INT) RETURNS INT BEGIN DECLARE num INT DEFAULT 0; DECLARE SUM INT DEFAULT 0; WHILE num<n DO SET num= num + 1; SET SUM=SUM + num; END WHILE; RETURN SUM; END// DELIMITER ; DELIMITER // CREATE FUNCTION test(n INT) RETURNS INT BEGIN DECLARE num INT DEFAULT 0; DECLARE SUM INT DEFAULT 0; labl:LOOP SET num=num+1; SET SUM=SUM+num; IF num>=n THEN LEAVE labl; END IF; END LOOP labl; RETURN SUM; END// DELIMITER ; DELIMITER // CREATE FUNCTION test(n INT) RETURNS INT BEGIN DECLARE num INT DEFAULT 0; DECLARE SUM INT DEFAULT 0; REPEAT SET num=num+1; SET SUM=SUM+num; UNTIL num>=n END REPEAT; RETURN SUM; END// DELIMITER ;
DELIMITER // (设置结束符 其实我也不太明白为啥要这样 记住就行把)
CREATE FUNCTION ym_date(mydate DATE) (创建函数 函数名字(参数))
RETURNS VARCHAR(20) (指定函数的返回类型)
BEGIN (函数开始,如果函数只有一句可以省略bigin和end)
RETURN DATE_FORMAT(mydate,%Y-%m); (函数体)
END// (函数结束)
DELIMITER ; (把结束符再变成;)
CREATE FUNCTION show_name_store(bid INT)
RETURNS VARCHAR(20)
RETURN (SELECT CONCAT_WS(‘-----‘,book_name,store) FROM bookinfo WHERE book_id=bid);
(into的介绍)
DELIMITER //
CREATE FUNCTION show_name_store(bid INT)
RETURNS VARCHAR(20)
BEGIN
DECLARE result VARCHAR(20) (声明变量)
SELECT CONCAT_WS(‘-----‘,book_name,store) INTO result FROM bookinfo WHERE book_id=bid;(对变量result进行赋值。emmm没啥说的)
RETURN result;
END//
DELIMITER ;
(删除函数)
DROP FUNCTION xxxxx;
(又是if else)
DELIMITER //
CREATE FUNCTION show_level(cid VARCHAR(20))
RETURNS VARCHAR(10)
BEGIN
DECLARE lev VARCHAR(20);
DECLARE money DECIMAL(7,3);
SELECT balance INTO money FROM readerinfo WHERE card_id=cid;
IF money>=500 THEN
SET lev=‘高级‘;
ELSEIF money>=300 THEN
SET lev=‘普通‘;
ELSE
SET lev=‘非会员‘;
END IF;
RETURN lev;
END//
DELIMITER ;
(case的样例)
DELIMITER //
CREATE FUNCTION show_lev2(cid CHAR(18))
RETURNS barchar(10)
BEGIN
DECLARE lev VARCHAR(10);
DECLARE money DECIMAL(7,3);
DECLARE num INT;
SELECT balance INTO money FROM readerinfo WHERE card_id=cid;
SET num=TRUNCATE(money/100,0);
CASE num
WHEN 0 THEN SET lev=‘非会员‘;
WHEN 1 THEN SET lev=‘普通‘;
WHEN 2 THEN SET lev=‘高级‘;
ELSE SET lev=‘金牌‘;
END CASE;
RETURN lev;
END//
DELIMITER ;
(CASE 也可以这样用)
CASE
WHEN money>500 THEN SET lev=‘高级‘;
WHEN money>=300 THEN SET lev=‘普通‘;
ELSE SET lev=‘不是‘;
END CASE;
(以下是循环的样例)
DELIMITER //
CREATE FUNCTION test(n INT)
RETURNS INT
BEGIN
DECLARE num INT DEFAULT 0;
DECLARE SUM INT DEFAULT 0;
WHILE num<n DO
SET num= num + 1;
SET SUM=SUM + num;
END WHILE;
RETURN SUM;
END//
DELIMITER ;
DELIMITER //
CREATE FUNCTION test(n INT)
RETURNS INT
BEGIN
DECLARE num INT DEFAULT 0;
DECLARE SUM INT DEFAULT 0;
labl:LOOP
SET num=num+1;
SET SUM=SUM+num;
IF num>=n THEN
LEAVE labl;
END IF;
END LOOP labl;
RETURN SUM;
END//
DELIMITER ;
DELIMITER //
CREATE FUNCTION test(n INT)
RETURNS INT
BEGIN
DECLARE num INT DEFAULT 0;
DECLARE SUM INT DEFAULT 0;
REPEAT
SET num=num+1;
SET SUM=SUM+num;
UNTIL num>=n
END REPEAT;
RETURN SUM;
END//
DELIMITER ;
原文地址:https://www.cnblogs.com/wpbing/p/9215588.html