MySQL--儲存函式程式碼筆記
阿新 • • 發佈:2018-11-11
mysql> select * from customers$$ +-----+-----------+----------+-----------+--------------+ | id | cust_name | cust_sex | cust_city | cust_address | +-----+-----------+----------+-----------+--------------+ | 901 | 馬騰 | F | 廣東 | 深圳 | | 902 | 張三 | F | 北京 | 朝陽 | | 903 | 李四 | M | 武漢 | 李家莊 | | 904 | 張無忌 | M | 光明頂 | 明教 | | 905 | 索隆 | M | 動漫 | 海賊王 | | 906 | 悟空 | F | 西遊 | 花果山 | | 909 | 萬華 | F | 長沙市 | 芙蓉區 | +-----+-----------+----------+-----------+--------------+ 7 rows in set (0.89 sec) mysql> create function fu_test(cid int) -> returns char(5) -> deterministic -> begin -> declare sex char(5); -> select cust_sex into sex from customers -> where id = cid; -> if sex is null then -> return(select '沒有該客戶'); -> else if sex = 'F' then -> return(select '女'); -> else return(select '男'); -> end if; -> end if; -> end $$ Query OK, 0 rows affected (0.00 sec) mysql> select fu_test(904); -> $$ +--------------+ | fu_test(904) | +--------------+ | 男 | +--------------+ 1 row in set (0.00 sec)