字符串函数:
concat():连接多个字符串
insert(str,x,y,instr):将字符串str从第x位置开始,y个字符长的子串替换为字符创instr
lower()
upper()
left(str,x):返回字符串str最左边的x个字符
right(str,x):返回字符串str最右边的x个字符
lpad()
rpad()
ltrim()
rtrim()
repeat(str,x):返回字符串str重复x次的结果
repleace()
strcmp(str1,str2):比较字符串str1和str2
trim()
substring()
合并字符串函数concat()和concat_ws()
concat_ws()函数全称为concat with separator,不仅将传入的其他参数连接起来,而且还会通过分隔符将各个字符分隔开。
mysql> select concat_ws('|','am','is','are');
+--------------------------------+
| concat_ws('|','am','is','are') |
+--------------------------------+
| am|is|are |
+--------------------------------+
1 row in set (0.00 sec)
获取字符串长度:
length()
获取字符数函数:
char_length()
查找字符串位置函数:find_in_set()、field()、locate()、position()、instr()
mysql> select find_in_set('Mysql','oracle,sqlserver,mysql,Mysql');
+-----------------------------------------------------+
| find_in_set('Mysql','oracle,sqlserver,mysql,Mysql') |
+-----------------------------------------------------+
| 3 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select field('mysql','oracle','sqlserver','mySQL','mysql');
+-----------------------------------------------------+
| field('mysql','oracle','sqlserver','mySQL','mysql') |
+-----------------------------------------------------+
| 3 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select locate('sql','mySQL') lo,position('sql' in 'mysql') po,instr('mysq
l','sql') ss ;
+----+----+----+
| lo | po | ss |
+----+----+----+
| 3 | 3 | 3 |
+----+----+----+
1 row in set (0.00 sec)
返回指定位置的字符串的elt()函数:
mysql> select elt(2,'oracle','sqlserver','mysql');
+-------------------------------------+
| elt(2,'oracle','sqlserver','mysql') |
+-------------------------------------+
| sqlserver |
+-------------------------------------+
1 row in set (0.00 sec)
获取字符串make_set()函数:
mysql> select bin(5),make_set(5,'mysql','oracle','sqlserver','kafka');
+--------+--------------------------------------------------+
| bin(5) | make_set(5,'mysql','oracle','sqlserver','kafka') |
+--------+--------------------------------------------------+
| 101 | mysql,sqlserver |
+--------+--------------------------------------------------+
1 row in set (0.00 sec)
数值函数:
abs(x):返回x的绝对值
ceil(x):返回大于x的最大整数值
floor(x):返回小于x的最大整数值
mod(x,y):返回x模y的值
rand():返回0-1内的随机数
round(x,y):返回数字x的四舍五入后有y位小数的数值
truncate(x,y):返回数字x截断为y位小数的数值
日期和时间函数:
curdate():获取当前日期
curtime():获取当前时间
now():获取当前的日期和时间 --推荐
unix_timestamp(date):获取日期date的unix时间戳
from_unixtime():获取unix时间戳的日期值
week(date):返回日期date为一年中的第几周
year()
hour(time)
minute(time)
monthname(date):返回时间date的月份值
mysql> select now(),year(now()),minute(now()),year(now()),monthname(now()),week(
now()) \G
*************************** 1. row ***************************
now(): 2018-04-18 19:07:31
year(now()): 2018
minute(now()): 7
year(now()): 2018
monthname(now()): April
week(now()): 15
1 row in set (0.00 sec)
extract()函数:获取日期和时间的各个部分
mysql> select now(),extract(year from now()) years,extract(month from now()) mon
ths,extract(day from now()) days, extract(hour from now()) hours,extract(minute
from now()) minutes,extract(second from now()) seconds;
+---------------------+-------+--------+------+-------+---------+---------+
| now() | years | months | days | hours | minutes | seconds |
+---------------------+-------+--------+------+-------+---------+---------+
| 2018-04-18 19:19:42 | 2018 | 4 | 18 | 19 | 19 | 42 |
+---------------------+-------+--------+------+-------+---------+---------+
1 row in set (0.00 sec)
datediff()函数:指定两个日期之间的相隔天数。
adddate(date,n):N天后
subdate(date,n):N天前
mysql> select datediff(now(),'1990-02-01');
+------------------------------+
| datediff(now(),'1990-02-01') |
+------------------------------+
| 10303 |
+------------------------------+
1 row in set (0.00 sec)
系统信息函数:
version():返回数据库的版本号
database():返回当前数据库名
user():返回当前用户
last_insert_id():返回最近生成的auto_increment值
mysql> select database(),user(),version();
+------------+----------------+------------+
| database() | user() | version() |
+------------+----------------+------------+
| test | root@localhost | 5.7.21-log |
+------------+----------------+------------+
1 row in set (0.00 sec)
其他函数:
if(value,t,f):如果value为真,返回t,否则返回f
ifnull(value1,value2):如果value1不为空返回value1,否则返回value2
case when [value1] then [result1]... else [default] end
case [expr] when [value1] then [result] .. else[default] end
password(str):对str加密
format(x,n):将数字x进行格式化,保留n位小数
inet_aton(ip):将IP地址转换成数字
inet_ntoa(x):将数字转换为IP地址
get_loct(name,time):创建一个持续时间为time的名为name的锁
release_loct(name):为名为name的锁进行解锁
benchmark(count,expr):将表达式重复执行count次
convert(s using cs):将字符串s的字符集变成cs
convert(x,type):将x变成type类型