常用關鍵字
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)