参考:https://www.techonthenet.com/mysql/functions/index.php
1.非统计函数
函数 | ||
---|---|---|
字符串函数 | concat(s1,s2...) | 拼接字符串 |
insert(str,x,y,instr) | 指定位置插入字符串 | |
lower(str) | ||
upper(str) | ||
left(str,x) | 取得左边的部分 | |
right(str,x) | ||
lpad(str,n,pad) | ||
rpad(str,n,pad) | ||
ltrim(str) | ||
rtrim(str) | ||
repeat(str,x) | ||
replace(str,a,b) | ||
strcmp(s1,s2) | ||
trim(str) | ||
substring(str,x,y) | ||
ASCII('t') | 字母转换为ASCII(编码 | |
数值函数 | abs(x) | |
cell(x) | ||
floor(x) | ||
mod(x,y) | ||
rand(x) | ||
round(x) | ||
DEGREES( number ) | 弧度转换为度数 | |
RADIANS( number ) | 度数转换为弧度 | |
SIN|COS|TAN|COT|.. | ||
LOG|LOG2|LOG10 | LOG( number ) 自然对数 或者LOG( base, number ) | |
POW|SQRT|EXP|LN | ||
日期和时间函数 | select curdate() | 2018-09-20 |
curtime() | 21:59:20 | |
now() | 2018-09-20 21:59:48 | |
unix_timestamp() | 1537451979 | |
from_unixtime(1537451979) | 2018-09-20 21:59:39 | |
week(now()) | 37 | |
year(now()) | 2018 | |
hour(now()) | 22 | |
minute(now()) | 1 | |
monthname(date) | ||
date_format(date,fmt) | https://www.techonthenet.com/mysql/functions/date_format.php | |
date_add(date,interval expr type) date_sub adddate subdate add_time sub_time period_add |
https://www.techonthenet.com/mysql/functions/date_add.php | |
datediff(expr,expr2) timediff period_diff |
https://www.techonthenet.com/mysql/functions/datediff.php | |
流程函数 | if(value,t,f) | |
ifnull(value1,value2) | ||
case when[value1]then[result1]...else[default]END |
||
其他常用函数 | DATABASE() | |
VERSION() | ||
USER() | ||
INET_ATON(ip) | ||
INET_NTOA(num) | ||
PASSWOD(str) | ||
MD5() | ||
CRC32 | ||
SHA1 | ||
高级函数 | SELECT CONV(5, 10, 2); | 进制转换 |
|
https://www.techonthenet.com/mysql/functions/last_insert_id.php | |
NULLIF | https://www.techonthenet.com/mysql/functions/nullif.php | |
2.统计函数
COUNT、SUM、MIM、MAX、AVG
SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];
-- 复杂
SELECT expression1, expression2, ... expression_n,
COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;