常用关键字
binary : 强制区分大小写。我们知道MySQL是不区分大小写的,我们可以使用binary
关键字来强制MySQL区分大小写
mysql> select username from user where username = 'laozhang';
+----------+
| username |
+----------+
| laozhang |
| laozhang |
| LAOZHANG |
+----------+
3 rows in set (0.00 sec)
mysql> select username from user where binary username = 'laozhang';
+----------+
| username |
+----------+
| laozhang |
| laozhang |
+----------+
2 rows in set (0.00 sec)
distinct : 最小化输出,每个唯一的输出记录一次。若搜索多列数据,则组合输出的唯一记录一次,并且无需在每列中添加关键字distinct
!
mysql> select username from user;
+----------+
| username |
+----------+
| laozhang |
| laoyang |
| laowang |
| laohe |
| laozhang |
| laoli |
| laoliang |
| laoyang |
+----------+
8 rows in set (0.00 sec)
mysql> select distinct username from user;
+----------+
| username |
+----------+
| laozhang |
| laoyang |
| laowang |
| laohe |
| laoli |
| laoliang |
+----------+
6 rows in set (0.00 sec)
# 2018-11-11加1个月
mysql> select "2018-11-11" + interval 1 month as date;
+------------+
| date |
+------------+
| 2018-12-11 |
+------------+
1 row in set (0.01 sec)
mysql> select "2018-11-11" - interval 1 month;
+---------------------------------+
| "2018-11-11" - interval 1 month |
+---------------------------------+
| 2018-10-11 |
+---------------------------------+
1 row in set (0.00 sec)
常用函数
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18 |
+-----------+
1 row in set (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
last_insert_id() : 获取上一次插入得到的ID
mysql> insert into student(name, age) values("laoliang", 20);
Query OK, 1 row affected (0.01 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 10 |
+------------------+
1 row in set (0.00 sec)
coalesce(value, …) : 返回列表中的第一个非空值,如果没有非空值,则返回null
# 如果age为空,则取score,若score也为空,则返回1
mysql> select coalesce(age, score, 1) from student;
greatest(value1, value2, …) : 使用两个或多个参数,返回最大值,若参数中包含null
,则返回null
。使用如下:
mysql> select greatest(1, 2, 0, 4, 3);
+-------------------------+
| greatest(1, 2, 0, 4, 3) |
+-------------------------+
| 4 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select greatest(1, 2, 0, null, 3);
+----------------------------+
| greatest(1, 2, 0, null, 3) |
+----------------------------+
| NULL |
+----------------------------+
1 row in set (0.00 sec)
isnull(expr) : 判断expr
是否为null
,为null
返回1,不为空返回0,使用如下:
mysql> select isnull(1 + 1), isnull(1 / 0), isnull(null);
+---------------+---------------+--------------+
| isnull(1 + 1) | isnull(1 / 0) | isnull(null) |
+---------------+---------------+--------------+
| 0 | 1 | 1 |
+---------------+---------------+--------------+
1 row in set (0.00 sec)
interval(N, N1, N2, N3, …) : 如果N < N1
则返回0,N < N2
返回1,N < N3
则返回2,以此类推。使用如下:
mysql> select interval(1, 2, 3, 4, 5), interval(2, 2, 3, 4, 5), interval(3, 2, 3, 4, 5), interval(4, 2, 3, 4, 5);
+-------------------------+-------------------------+-------------------------+-------------------------+
| interval(1, 2, 3, 4, 5) | interval(2, 2, 3, 4, 5) | interval(3, 2, 3, 4, 5) | interval(4, 2, 3, 4, 5) |
+-------------------------+-------------------------+-------------------------+-------------------------+
| 0 | 1 | 2 | 3 |
+-------------------------+-------------------------+-------------------------+-------------------------+
1 row in set (0.01 sec)
least(value1, value2, …) : 使用两个或多个参数,返回最小值参数。如果有任何参数为null
,则结果为null
。使用如下:
mysql> select least(1, 0.1, 2, 4, 3);
+------------------------+
| least(1, 0.1, 2, 4, 3) |
+------------------------+
| 0.1 |
+------------------------+
1 row in set (0.00 sec)
mysql> select least(1, 0.1, 2, 4, null);
+---------------------------+
| least(1, 0.1, 2, 4, null) |
+---------------------------+
| NULL |
+---------------------------+
1 row in set (0.00 sec)
concat(str1, str2, …) : 返回参数连接所产生的字符串,如果参数包含null
,则返回null
。使用如下:
mysql> select concat(19.04, 'laozhang'), concat(19.04), concat('a', 'b', null, 'c');
+---------------------------+---------------+-----------------------------+
| concat(19.04, 'laozhang') | concat(19.04) | concat('a', 'b', null, 'c') |
+---------------------------+---------------+-----------------------------+
| 19.04laozhang | 19.04 | NULL |
+---------------------------+---------------+-----------------------------+
1 row in set (0.00 sec)
concat_ws(separator, str1, str2, …) : 一个特殊形式的concat
,第一个参数为分隔符。使用如下:
mysql> select concat_ws(",", 19.04, 'hhaha');
+--------------------------------+
| concat_ws(",", 19.04, 'hhaha') |
+--------------------------------+
| 19.04,hhaha |
+--------------------------------+
1 row in set (0.00 sec)
elt(n, str1, str2, …) : 返回字符串列表中的第n
个字符串,如n=1
,则返回str1
,如n=2
,则返回str2
,以此类推。使用如下:
mysql> select elt(1, 'a', 'c', 'd', 'b'), elt(2, 'a', 'c', 'd', 'b'), elt(0, 'a', 'c', 'd', 'b');
+----------------------------+----------------------------+----------------------------+
| elt(1, 'a', 'c', 'd', 'b') | elt(2, 'a', 'c', 'd', 'b') | elt(0, 'a', 'c', 'd', 'b') |
+----------------------------+----------------------------+----------------------------+
| a | c | NULL |
+----------------------------+----------------------------+----------------------------+
1 row in set (0.00 sec)
field(str, str1, str2, str3, str4, …) : 如果str=str1
,返回1,str=str2
,返回2,以此类推;如果str
为null
,则返回0, 如果未找到相等的也等于0。使用如下:
mysql> select field('a', 'a', 'c', 'b'), field('b', 'a', 'c', 'b'), field('d', 'a', 'c', 'b'), field(null, 'a', 'c', 'b', null);
+---------------------------+---------------------------+---------------------------+----------------------------------+
| field('a', 'a', 'c', 'b') | field('b', 'a', 'c', 'b') | field('d', 'a', 'c', 'b') | field(null, 'a', 'c', 'b', null) |
+---------------------------+---------------------------+---------------------------+----------------------------------+
| 1 | 3 | 0 | 0 |
+---------------------------+---------------------------+---------------------------+----------------------------------+
1 row in set (0.00 sec)
find_in_set(str, strlist) : 获取str
串在strlist
字符串的位置,1为起始位置,找不到返回0;strlist
为多个字符串以逗号隔开连接的字符串;如果str
或者strlist
中有一个为空,则返回空。使用如下:
mysql> select find_in_set("a", "a,b,c"), find_in_set("d", "a,b,c"), find_in_set(null, "a,b,c");
+---------------------------+---------------------------+----------------------------+
| find_in_set("a", "a,b,c") | find_in_set("d", "a,b,c") | find_in_set(null, "a,b,c") |
+---------------------------+---------------------------+----------------------------+
| 1 | 0 | NULL |
+---------------------------+---------------------------+----------------------------+
1 row in set (0.00 sec)
format(x, d) : 格式化字符串,返回一个字符串,d
为保留的小数位数。使用如下:
mysql> select format(19.04, 1), format(19.05, 1);
+------------------+------------------+
| format(19.04, 1) | format(19.05, 1) |
+------------------+------------------+
| 19.0 | 19.1 |
+------------------+------------------+
1 row in set (0.00 sec)
insert(str, pos, len, newstr) : 返回字符串str
, 在pos
开始长度为len
的字符替换成为newstr
。如果pos
不在字符串的长度内,则返回原始字符串;如果pos
开始len
长度中,不在字符串的长度内,则从位置替换字符串的其余部分。使用如下:
mysql> select insert("123456", 2, 2, "what"), insert("123456", -1, 4, "what"), insert("123456", 2, 6, "what");
+--------------------------------+---------------------------------+--------------------------------+
| insert("123456", 2, 2, "what") | insert("123456", -1, 4, "what") | insert("123456", 2, 6, "what") |
+--------------------------------+---------------------------------+--------------------------------+
| 1what456 | 123456 | 1what |
+--------------------------------+---------------------------------+--------------------------------+
1 row in set (0.00 sec)
instr(str, substr) : 返回substr
字符串在str
字符串中第一次出现的位置,不存在返回0。使用如下:
mysql> select instr('basketball', 'ball'), instr('football', 'basket');
+-----------------------------+-----------------------------+
| instr('basketball', 'ball') | instr('football', 'basket') |
+-----------------------------+-----------------------------+
| 7 | 0 |
+-----------------------------+-----------------------------+
1 row in set (0.00 sec)
lcase(str) : 将str
的小写形式返回,与lower()
作用一样。使用如下:
mysql> select lcase('JDK'), lcase(''), lcase(null);
+--------------+-----------+-------------+
| lcase('JDK') | lcase('') | lcase(null) |
+--------------+-----------+-------------+
| jdk | | NULL |
+--------------+-----------+-------------+
1 row in set (0.00 sec)
left(str, len) : 在str
字符串中从最左边开始截取len
个字符返回,str
或len
为null
,则返回null
。使用如下:
mysql> select left("abcdefg", 2), left("abcdefg", -2), left("abcdefg", null), left(null, 2);
+--------------------+---------------------+-----------------------+---------------+
| left("abcdefg", 2) | left("abcdefg", -2) | left("abcdefg", null) | left(null, 2) |
+--------------------+---------------------+-----------------------+---------------+
| ab | | NULL | NULL |
+--------------------+---------------------+-----------------------+---------------+
1 row in set (0.00 sec)
length(str) : 获取str
字符串的长度,str
为null
则返回null
,使用如下:
mysql> select length('abcdefg'), length(null), length(0);
+-------------------+--------------+-----------+
| length('abcdefg') | length(null) | length(0) |
+-------------------+--------------+-----------+
| 7 | NULL | 1 |
+-------------------+--------------+-----------+
1 row in set (0.01 sec)
locate(substr, str), locate(substr, str, pos) : 第一种:获取substr
在str
中第一次出现的位置;第二种:获取substr
在str
中从pos
位置开始第一次出现的位置。如果substr
不存在,则返回0,如果substr
活着str
为null
,则返回null
。第一种方式功能等同于instr
。使用如下:
mysql> select locate('asd', '123asd123asd'), locate('df', '123asd123asd'), locate(null, '123asd123asd'), locate('df', null), locate('asd', '123asd123asd', 11);
+-------------------------------+------------------------------+------------------------------+--------------------+-----------------------------------+
| locate('asd', '123asd123asd') | locate('df', '123asd123asd') | locate(null, '123asd123asd') | locate('df', null) | locate('asd', '123asd123asd', 11) |
+-------------------------------+------------------------------+------------------------------+--------------------+-----------------------------------+
| 4 | 0 | NULL | NULL | 0 |
+-------------------------------+------------------------------+------------------------------+--------------------+-----------------------------------+
1 row in set (0.00 sec)
lower(str) : 将str
字符串的小写形式返回,使用如下:
mysql> select lower('JDK'), lower(''), lower(null);
+--------------+-----------+-------------+
| lower('JDK') | lower('') | lower(null) |
+--------------+-----------+-------------+
| jdk | | NULL |
+--------------+-----------+-------------+
1 row in set (0.00 sec)
ltrim(str) : 去除字符串str
左边的空格后返回,使用如下:
mysql> select ltrim(' foot ball ');
+-------------------------+
| ltrim(' foot ball ') |
+-------------------------+
| foot ball |
+-------------------------+
1 row in set (0.01 sec)
mid(str, pos, len) : 字符串截取,从pos
位置开始,截取len
个长度,效果与substring()
方法一样。使用如下:
mysql> select mid('hello world', 1, 5), mid('hello world', 3, 6);
+--------------------------+--------------------------+
| mid('hello world', 1, 5) | mid('hello world', 3, 6) |
+--------------------------+--------------------------+
| hello | llo wo |
+--------------------------+--------------------------+
1 row in set (0.00 sec)
position(substr in str) : 获取substr
在str
字符串中第一次出现的位置。效果与locate()
和instr()
方法一样,使用如下:
mysql> select position('asd' in '123asd123asd'), position('df' in '123asd123asd'), position(null in '123asd123asd'), position('asd' in null);
+-----------------------------------+----------------------------------+----------------------------------+-------------------------+
| position('asd' in '123asd123asd') | position('df' in '123asd123asd') | position(null in '123asd123asd') | position('asd' in null) |
+-----------------------------------+----------------------------------+----------------------------------+-------------------------+
| 4 | 0 | NULL | NULL |
+-----------------------------------+----------------------------------+----------------------------------+-------------------------+
1 row in set (0.00 sec)
repeat(str, count) : 返回由str
重复count
次数的字符串组成的新字符串。如果count
小于或等于0,将返回空字符串;str
或count
为null
,则返回null
,使用如下:
mysql> select repeat('mysql', 3), repeat('mysql', -1), repeat('mysql', 0), repeat(null, 2), repeat('mysql', null);
+--------------------+---------------------+--------------------+-----------------+-----------------------+
| repeat('mysql', 3) | repeat('mysql', -1) | repeat('mysql', 0) | repeat(null, 2) | repeat('mysql', null) |
+--------------------+---------------------+--------------------+-----------------+-----------------------+
| mysqlmysqlmysql | | | NULL | NULL |
+--------------------+---------------------+--------------------+-----------------+-----------------------+
1 row in set (0.01 sec)
replace(str, from_str, to_str) : 将str
字符串中的所有from_str
替换成为to_str
后返回。如果from_str
不在str
字符串中存在,则原样返回;如果str
、from_str
和to_str
中有一个字符串为null
,则返回null
。使用如下:
mysql> select replace('123asd123asd', 'asd', 'ggg'), replace('123asd123asd', '-1', '30'), replace(null, 'asd', 'ggg'), replace('123asd123asd', null, 'ggg'), replace('123asd123sad', 'asd', null);
+---------------------------------------+-------------------------------------+-----------------------------+--------------------------------------+--------------------------------------+
| replace('123asd123asd', 'asd', 'ggg') | replace('123asd123asd', '-1', '30') | replace(null, 'asd', 'ggg') | replace('123asd123asd', null, 'ggg') | replace('123asd123sad', 'asd', null) |
+---------------------------------------+-------------------------------------+-----------------------------+--------------------------------------+--------------------------------------+
| 123ggg123ggg | 123asd123asd | NULL | NULL | NULL |
+---------------------------------------+-------------------------------------+-----------------------------+--------------------------------------+--------------------------------------+
1 row in set (0.00 sec)
mysql> select reverse('abc'), reverse('123');
+----------------+----------------+
| reverse('abc') | reverse('123') |
+----------------+----------------+
| cba | 321 |
+----------------+----------------+
1 row in set (0.00 sec)
right(str, len) : 返回str
字符串中最右边的len
长度的字符。如果len
不在str
字符串的长度内,则返回空字符串;如果str
或者count
为null
,则返回null
。使用如下:
mysql> select right('football', 4), right('football', 0), right('football', -1), right('football', 10), right('football', null), right(null, 4);
+----------------------+----------------------+-----------------------+-----------------------+-------------------------+----------------+
| right('football', 4) | right('football', 0) | right('football', -1) | right('football', 10) | right('football', null) | right(null, 4) |
+----------------------+----------------------+-----------------------+-----------------------+-------------------------+----------------+
| ball | | | football | NULL | NULL |
+----------------------+----------------------+-----------------------+-----------------------+-------------------------+----------------+
1 row in set (0.00 sec)
rtrim(str) : 去除str字符串右边的空格而后返回,str
为null
则返回null
。使用如下:
mysql> select rtrim('foot ball '), rtrim(null);
+----------------------+-------------+
| rtrim('foot ball ') | rtrim(null) |
+----------------------+-------------+
| foot ball | NULL |
+----------------------+-------------+
1 row in set (0.00 sec)
space(n) : 返回n
个空格,如果n
为null
则返回null
。使用如下:
mysql> select space(5), space(null);
+----------+-------------+
| space(5) | space(null) |
+----------+-------------+
| | NULL |
+----------+-------------+
1 row in set (0.01 sec)
substring(str, pos), substring(str from pos), substring(str, pos, len), substring(str from pos for len) : 字符串截取,如果len
小于1,将会返回空字符串
substring(str, pos)
:在pos
位置开始截取,到末尾substring(str from pos)
:在pos
位置开始截取,到末尾substring(str, pos, len)
:在pos
位置开始截取,截取len
个长度substring(str from pos for len)
:在pos
位置开始截取,截取len
个长度
mysql> select substring('abcdefg', 2), substring('abcdefg' from 2), substring('abcdefg', 2, 2), substring('abcdefg' from 2 for 2);
+-------------------------+-----------------------------+----------------------------+-----------------------------------+
| substring('abcdefg', 2) | substring('abcdefg' from 2) | substring('abcdefg', 2, 2) | substring('abcdefg' from 2 for 2) |
+-------------------------+-----------------------------+----------------------------+-----------------------------------+
| bcdefg | bcdefg | bc | bc |
+-------------------------+-----------------------------+----------------------------+-----------------------------------+
1 row in set (0.00 sec)
substr(str, pos), substr(str from pos), substr(str, pos, len), substr(str from pos for len) :字符串截取,效果与substring()
一样substring_index(str, delim, count) :str
字符串中delim
出现count
次位置开始截取,如果count
为正数,往左边截取;如果为负数,往右边开始截取;如果为零,则返回空字符串。使用如下:
mysql> select substring_index('www.baidu.com', '.', 2), substring_index('www.baidu.com', '.', '-2'), substring_index('www.baidu.com', '.', 0);
+------------------------------------------+---------------------------------------------+------------------------------------------+
| substring_index('www.baidu.com', '.', 2) | substring_index('www.baidu.com', '.', '-2') | substring_index('www.baidu.com', '.', 0) |
+------------------------------------------+---------------------------------------------+------------------------------------------+
| www.baidu | baidu.com | |
+------------------------------------------+---------------------------------------------+------------------------------------------+
1 row in set (0.00 sec)
trim(str), trim(remstr from str), trim(leading remstr from str), trim(trailing remstr from str), trim(both remstr from str) : 去除空格或指定字符串
trim(str)
:去除str
字符串中左右两边的空格trim(remstr from str)
:去除str
字符串中左右两边的字符串remstr
trim(leading remstr from str)
:去除str
字符串中最左边的字符串remstr
trim(trailing remstr from str)
:去除str
字符串中最右边的字符串remstr
trim(both remstr from str)
:去除str
字符串中左右两边的字符串remstr
。效果与trim(remstr from str)
一样
msyql> select trim(' a b '), trim('123' from '123a123a123'), trim(leading '123' from '123a123a123'), trim(trailing '123' from '123a123a123'), trim(both '123' from '123a123a123');
+---------------+--------------------------------+----------------------------------------+-----------------------------------------+-------------------------------------+
| trim(' a b ') | trim('123' from '123a123a123') | trim(leading '123' from '123a123a123') | trim(trailing '123' from '123a123a123') | trim(both '123' from '123a123a123') |
+---------------+--------------------------------+----------------------------------------+-----------------------------------------+-------------------------------------+
| a b | a123a | a123a123 | 123a123a | a123a |
+---------------+--------------------------------+----------------------------------------+-----------------------------------------+-------------------------------------+
1 row in set (0.00 sec)
ucase(str) : 将str
字符串的大写形式返回,使用如下:
mysql> select ucase('asd');
+--------------+
| ucase('asd') |
+--------------+
| ASD |
+--------------+
1 row in set (0.00 sec)
upper(str) : 将str
字符串的大写形式返回,使用如下
mysql> select upper('asd');
+--------------+
| upper('asd') |
+--------------+
| ASD |
+--------------+
1 row in set (0.00 sec)
strcmp(expr1, expr2) : 如果expr1
与expr2
相同,则返回0;如果expr1
比expr2
小,则返回-1;如果expr1
比expr2
大,则返回1。使用如下:
mysql> select strcmp('0', '0'), strcmp('test', 'test'), strcmp('0', '1'), strcmp('test', 'test1'), strcmp('1', '0'), strcmp('test1', 'test');
+------------------+------------------------+------------------+-------------------------+------------------+-------------------------+
| strcmp('0', '0') | strcmp('test', 'test') | strcmp('0', '1') | strcmp('test', 'test1') | strcmp('1', '0') | strcmp('test1', 'test') |
+------------------+------------------------+------------------+-------------------------+------------------+-------------------------+
| 0 | 0 | -1 | -1 | 1 | 1 |
+------------------+------------------------+------------------+-------------------------+------------------+-------------------------+
1 row in set (0.00 sec)
mysql> select abs(-2), abs(-32);
+---------+----------+
| abs(-2) | abs(-32) |
+---------+----------+
| 2 | 32 |
+---------+----------+
1 row in set (0.00 sec)
mysql> select ceiling(1.32), ceiling(-1.32);
+---------------+----------------+
| ceiling(1.32) | ceiling(-1.32) |
+---------------+----------------+
| 2 | -1 |
+---------------+----------------+
1 row in set (0.00 sec)
ceil(x) : 返回不小于x
的最小整数,效果与ceiling()
方法一样mod(n, m), n % m, n mod m : 取模运算
mysql> select mod(9, 2), 9 % 2, 9 mod 2;
+-----------+-------+---------+
| mod(9, 2) | 9 % 2 | 9 mod 2 |
+-----------+-------+---------+
| 1 | 1 | 1 |
+-----------+-------+---------+
1 row in set (0.00 sec)
rand() : 返回一个0 <= value < 1
的随机浮点数。使用如下:
mysql> select rand(), rand();
+--------------------+-------------------+
| rand() | rand() |
+--------------------+-------------------+
| 0.2508203557234447 | 0.608450261511328 |
+--------------------+-------------------+
1 row in set (0.00 sec)
round(x), round(x, d) : 四舍五入运算
round(x)
:保留整数位round(x, d)
:将x
保留d
位小数
mysql> select round(1.4), round(1.5), round(1.24, 1), round(1.25, 1), round(1.25, 0);
+------------+------------+----------------+----------------+----------------+
| round(1.4) | round(1.5) | round(1.24, 1) | round(1.25, 1) | round(1.25, 0) |
+------------+------------+----------------+----------------+----------------+
| 1 | 2 | 1.2 | 1.3 | 1 |
+------------+------------+----------------+----------------+----------------+
1 row in set (0.00 sec)
mysql> select sqrt(4), sqrt(9), sqrt(2), sqrt(-16);
+---------+---------+--------------------+-----------+
| sqrt(4) | sqrt(9) | sqrt(2) | sqrt(-16) |
+---------+---------+--------------------+-----------+
| 2 | 3 | 1.4142135623730951 | NULL |
+---------+---------+--------------------+-----------+
1 row in set (0.00 sec)
truncate(x, d) : 截断x
到小数点后d
位。使用如下:
mysql> select truncate(1.23, 1), truncate(1.25, 1);
+-------------------+-------------------+
| truncate(1.23, 1) | truncate(1.25, 1) |
+-------------------+-------------------+
| 1.2 | 1.2 |
+-------------------+-------------------+
1 row in set (0.01 sec)
常用属性
mysql> select current_date;
+--------------+
| current_date |
+--------------+
| 2018-11-10 |
+--------------+
1 row in set (0.00 sec)
mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 10:22:15 |
+--------------+
1 row in set (0.00 sec)
mysql> select localtime;
+---------------------+
| localtime |
+---------------------+
| 2019-01-02 13:41:47 |
+---------------------+
1 row in set (0.01 sec)
localtimestamp : 获取当前日期时间,作用与localtime
一样
mysql> select localtimestamp;
+---------------------+
| localtimestamp |
+---------------------+
| 2019-01-02 13:42:11 |
+---------------------+
1 row in set (0.00 sec)