標籤:
1、字元函數
CONCAT() 串連字元
mysql> select concat(‘a‘,‘b‘,‘c‘);+---------------------+| concat(‘a‘,‘b‘,‘c‘) |+---------------------+
CONCAT_WS() 用指定的分隔字元串連
mysql> select concat_ws(‘-‘,‘a‘,‘b‘,‘c‘);+----------------------------+| concat_ws(‘-‘,‘a‘,‘b‘,‘c‘) |+----------------------------+| a-b-c |+----------------------------+
FORMAT() 數字格式化
mysql> select format(1234.567,2);+--------------------+| format(1234.567,2) |+--------------------+| 1,234.57 |+--------------------+
LOWER() 字母轉化為小寫
mysql> select lower(‘ASDFGH‘);+-----------------+| lower(‘ASDFGH‘) |+-----------------+| asdfgh |+-----------------+
UPPER() 字母轉化為大寫
mysql> select upper(‘asdfgh‘);+-----------------+| upper(‘asdfgh‘) |+-----------------+| ASDFGH |+-----------------+
LEFT() 擷取左側字元
mysql> select left(‘asdfgh‘,3);+------------------+| left(‘asdfgh‘,3) |+------------------+| asd |+------------------+
RIGHT() 擷取右側字元
+-------------------+| right(‘asdfgh‘,3) |+-------------------+| fgh |+-------------------+
LENGTH() 擷取字元長度
mysql> select length(‘asdfgh‘);+------------------+| length(‘asdfgh‘) |+------------------+| 6 |+------------------+
LTRIM() 刪除前置空格
+------------------+| ltrim(‘ china‘) |+------------------+| china |+------------------+
RTRIM() 刪除後導空格
+----------------------+| rtrim(‘ china ‘) |+----------------------+| china |+----------------------+
TRIM() 刪除前置和後導空格或指定字元
mysql> select trim(‘ china ‘);+---------------------+| trim(‘ china ‘) |+---------------------+| china |+---------------------+
mysql> select trim(leading‘a‘ from ‘aaaaab‘);+--------------------------------+| trim(leading‘a‘ from ‘aaaaab‘) |+--------------------------------+| b |+--------------------------------+
mysql> select trim(trailing ‘a‘ from ‘baaaaa‘);+----------------------------------+| trim(trailing ‘a‘ from ‘baaaaa‘) |+----------------------------------+| b |+----------------------------------+
REPLACE() 字元替換
mysql> select replace(‘aaaab‘,‘a‘,‘b‘);+--------------------------+| replace(‘aaaab‘,‘a‘,‘b‘) |+--------------------------+| bbbbb |+--------------------------+
SUBSTRING() 截取字元
mysql> select substring(‘china‘,3);+----------------------+| substring(‘china‘,3) |+----------------------+| ina |+----------------------+
[NOT] LIKE() 模式比對
2、數值運算與函數
+、-、*、/ 就不多說了
CEIL() 進一取整
mysql> select ceil(12345.34);+----------------+| ceil(12345.34) |+----------------+| 12346 |+----------------+
FLOOR() 舍一取整
mysql> select floor(12345.34);+-----------------+| floor(12345.34) |+-----------------+| 12345 |+-----------------+
MOD 取餘數,相當於%
mysql> select 9 mod 3;+---------+| 9 mod 3 |+---------+| 0 |+---------+
DIV 整數除法,相當於/
mysql> select 9 div 3;+---------+| 9 div 3 |+---------+| 3 |+---------+
POWER() 冪遠算
mysql> select power(2,3);+------------+| power(2,3) |+------------+| 8 |+------------+
ROUND() 四捨五入
mysql> select round(12345,345);+------------------+| round(12345,345) |+------------------+| 12345 |+------------------+
TRUNCATE() 數字截取
mysql> select truncate(12345.3456,-1);+-------------------------+| truncate(12345.3456,-1) |+-------------------------+| 12340 |+-------------------------+
3、比較子函數
[NOT] BETWEEN .. AND .. 判斷是否在某區間
[NOT] .. IN ..判斷是否包括在某數組中
IS [NOT] NULL 判斷是否為空白
4、日期時間函數
NOW() 顯示當前日期時間
CURDATE() 當前日期
CURTIME() 目前時間
DATE_ADD() 日期時間變更(可增減)
DATEDIEF() 日期時間的時間差
DATE_FORMAT() 日期時間格式化
5、資訊函數
CONNECTION_ID() 顯示串連ID
DATEBASE() 當前開啟的資料庫
LAST_INSERT_ID() 顯示最後插入的記錄
USER() 顯示目前使用者
VERSION() 顯示版本號碼
6、彙總函式
AVG() 平均值
COUNT() 計數
MAX() 最大值
MIN() 最小值
SUM() 求和
7、自訂函數
CREATE FUNCTION fun_name
RETURNS 傳回值的類型
routine_body
例:CREATE FUNCTION adduser(username VARCHAR(20))
RETURNS INT UNSIGNED
BEGIN
INSERT test(username) VALUES(username)
RETURN
LAST_INSERT_ID()
END
DORP fun_name(); 刪除自訂函數
DELIMITER 符號; 指定‘符號’為分割符
MySQL常用函數