詳解資料庫_MySQL: mysql函式
阿新 • • 發佈:2020-01-09
一.內建函式
1.數學函式
rand() | round(num) | ceil(num) | floor(num) |
---|---|---|---|
隨機 | 四捨五入 | 向上取整 | 向下取整 |
2.字串函式
length() 位元組長度
char_length() 字元長度
ucase() 大寫
lcase() 小寫
concat(字元,…,字元n) 連線字串
replace(字串,舊字元,新字元)字串替換
擷取字串
left(字串,擷取長度)
right(字串,擷取長度)
substring(字串,開始位置,擷取長度) #包含開始位置
mysql> select left('123456',4); +------------------+ | left('123456',4) | +------------------+ | 1234 | +------------------+ 1 row in set (0.00 sec) mysql> select right('123456',4); +-------------------+ | right('123456',4) | +-------------------+ | 3456 | +-------------------+ 1 row in set (0.00 sec) mysql> select substring('123456',2,4); +-------------------------+ | substring('123456',4) | +-------------------------+ | 2345 | +-------------------------+ 1 row in set (0.00 sec)
3.日期函式
now() | unix_timestamp() | from_unixtime() |
---|---|---|
當前時間 | 時間戳 | 格式化時間戳 |
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-03-16 14:55:42 | +---------------------+ 1 row in set (0.00 sec) mysql> select unix_timestamp(); +------------------+ | unix_timestamp() | +------------------+ | 1552719356 | +------------------+ 1 row in set (0.00 sec) mysql> select from_unixtime(1552719356); +---------------------------+ | from_unixtime(1552719356) | +---------------------------+ | 2019-03-16 14:55:56 | +---------------------------+ 1 row in set (0.00 sec)
year() | month() | day() | hour() | minute() | second() |
---|---|---|---|---|---|
年 | 月 | 日 | 時 | 分 | 秒 |
mysql> select -> year(now()) as '年',-> month(now()) as '月',-> day(now()) as '日',-> hour(now()) as '時',-> minute(now()) as '分',-> second(now()) as '秒'; +------+------+------+------+------+------+ | 年 | 月 | 日 | 時 | 分 | 秒 | +------+------+------+------+------+------+ | 2019 | 3 | 16 | 14 | 59 | 12 | +------+------+------+------+------+------+
4.加密函式
md5(資料)
password(資料)
5.條件判斷函式
1).語法: if(資料,值1,值2) #判斷指定資料是否為真:真-值1,假-值2
mysql> select if(null,1,2); +--------------+ | if(null,2) | +--------------+ | 2 | +--------------+ 1 row in set (0.00 sec) mysql> select if(1,2); +-----------+ | if(1,2) | +-----------+ | 0 | +-----------+ 1 row in set (0.00 sec)
2).語法: IFNULL(資料,值2) #判斷指定資料是否為null:null-值2,非null-本身
mysql> select ifnull(0,123); +---------------+ | ifnull(0,123) | +---------------+ | 0 | +---------------+ 1 row in set (0.00 sec) mysql> select ifnull('a',123); +-----------------+ | ifnull('a',123) | +-----------------+ | a | +-----------------+ 1 row in set (0.00 sec)
二.自定義函式
語法:
#修改結束符 delimiter // create function 函式名(引數名 型別,...,引數名n 型別n) returns 返回資料型別 begin #SQL語句 return 返回值; end // delimiter ; #呼叫 select 函式名();
輸出"hello world"(不帶引數的函式)
#判斷函式是否存在,存在就刪除 drop function if exists f1; delimiter // create function f1() returns varchar(30) begin return 'hello world'; end // delimiter ; select f1(); +-------------+ | f1() | +-------------+ | hello world | +-------------+
傳遞兩個整型求和(帶引數的函式)
drop function if exists f2; delimiter // create function f2(num1 int,num2 int) returns int begin return num1 + num2; end // delimiter ; select f2(8,2); +----------+ | f2(8,2) | +----------+ | 10 | +----------+
三.自定義函式相關語法
顯示所有的函式:show function status\G #輸出的內容很多
刪除函式:drop function [if exists] 函式名;
四.儲存過程和函式的區別
儲存過程可以返回多個值,而自定義函式只能返回一個值
儲存過程一般獨立執行,而函式往往作為其他SQL語句的一部分來使用
以上所述是小編給大家介紹的資料庫_MySQL: mysql函式詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!