第07章单行函数
2. 数值函数
2.4 指数函数、对数函数
函数 | 用法 |
---|---|
POW(x,y),POWER(X,Y) | 返回x的y次方 |
EXP(X) | 返回e的x次方,其中e是一个常数,2.718281828459045 |
LN(X),LOG(X) | 返回以e为底的X的对数,当x<=0时,返回的结果为NULL |
LOG10(X) | 返回以10为底的X的对数,当X<=0时,返回的结果为NULL |
LOG2(X) | 返回以2为底的x的对数,当X<=0时,返回NULL |
举例:
#指数
SELECT POW(2,5),POWER(2,4),EXP(2)
FROM DUAL;
#输出
+----------+------------+------------------+
| POW(2,5) | POWER(2,4) | EXP(2) |
+----------+------------+------------------+
| 32 | 16 | 7.38905609893065 |
+----------+------------+------------------+
1 row in set (0.00 sec)
#对数
SELECT LN(EXP(2)),LOG(EXP(2)),LOG10(10),LOG2(4)
FROM DUAL;
#输出
+------------+-------------+-----------+---------+
| LN(EXP(2)) | LOG(EXP(2)) | LOG10(10) | LOG2(4) |
+------------+-------------+-----------+---------+
| 2 | 2 | 1 | 2 |
+------------+-------------+-----------+---------+
1 row in set (0.00 sec)
2.5 进制间的转换
函数 | 用法 |
---|---|
BIN(x) | 返回x的二进制编码 |
HEX(x) | 返回x的十六进制编码 |
OCT(x) | 返回x的八进制编码 |
CONV(x,f1,f2) | 返回f1进制数变成f2进制数 |
举例:
#进制间的转换
SELECT BIN(10),HEX(10),OCT(10),CONV(10,10,8)
FROM DUAL;
#输出
+---------+---------+---------+---------------+
| BIN(10) | HEX(10) | OCT(10) | CONV(10,10,8) |
+---------+---------+---------+---------------+
| 1010 | A | 12 | 12 |
+---------+---------+---------+---------------+
1 row in set (0.01 sec)
3. 字符串函数
函数 | 用法 |
---|---|
ASCIl(S) | 返回字符串S中的第一个字符的ASCII码值 |
CHAR_LENGTH(s) | 返回字符串s的字符数。作用与CHARACTER_LENGTH(s)相同 |
LENGTH(s) | 返回字符串s的字节数,和字符集有关 |
CONCAT(s1,s2…sn) | 连接s1,s2…,sn为一个字符串 |
CONCAT_WS(x,s1,s2,.sn) | 同CONCAT(s1,s2,…sn)函数,但是每个字符串之间要加上x |
INSERT(str, idx, len,replacestr) | 将字符串str从第idx位置开始,len个字符长的子串替换为字符串replacestr |
REPLACE(str, a, b) | 用字符串b替换字符串str中所有出现的字符串a |
UPPER(s)或UCASE(s) | 将字符串s的所有字母转成大写字母 |
LOWER(s)或LCASE(s) | 将字符串s的所有字母转成小写字母 |
LEFT(str,n) | 返回字符串str最左边的n个字符 |
RIGHT(str,n) | 返回字符串str最右边的n个字符 |
LPAD(str, len, pad) | 用字符串pad对str最左边进行填充,直到str的长度为len个字符 |
RPAD(str ,len, pad) | 用字符串pad对str最右边进行填充,直到str的长度为len个字符 |
LTRIM(s) | 去掉字符串s左侧的空格 |
RTRIM(s) | 去掉字符串s右侧的空格 |
TRIM(s) | 去掉字符串s开始与结尾的空格 |
TRIM(s1 FROM s) | 去掉字符串s开始与结尾的s1 |
TRIM(LEADING s1 FROM s) | 去掉字符串s开始处的s1 |
TRIM(TRAILING s1 FROM s) | 去掉字符串s结尾处的s1 |
REPEAT(str, n) | 返回str重复n次的结果 |
SPACE(n) | 返回n个空格 |
STRCMP(s1,s2) | 比较字符串s1,s2的ASClI码值的大小 |
SUBSTR(s,index,len) | 返回从字符串s的index位置其len个字符,作用与SUBSTRING(s,n,len)、MID(s,n,len)相同 |
LOCATE(substr,str) | 返回字符串substr在字符串str中首次出现的位置,作用于POSITION(substr IN str)、INSTR(str,substr)相同。未找到返回0 |
ELT(m,1,s2,…n) | 返回指定位置的字符串,如果m=1,则返回s1,如果m=2,则返回s2,如果m=n,则返回sn |
FIELD(s,s1,s2…n) | 返回字符串s在字符串列表中第一次出现的位置 |
FIND_IN_SET(s1,s2) | 返回字符串s1在字符串s2中出现的位置。其中,字符串s2是一个以逗号分隔的字符串 |
REVERSE(s) | 返回s反转后的字符串 |
NULLIF(value1,value2) | 比较两个字符串,如果value1与value2相等,则返回NULL,否则返回value1 |
注意:MySQL中,字符串的位置是从1开始的。
举例:
#2. 字符串函数
#ASCII:求字符串中第一个字符的ASCII码
#CHAR_LENGTH:求字符串长度CHAR_LENGTH
#LENGTH:字符串所占字节数
SELECT ASCII('Abcdfsf'),CHAR_LENGTH('hello'),CHAR_LENGTH('我们')
FROM DUAL;
#输出
+------------------+----------------------+---------------------+
| ASCII('Abcdfsf') | CHAR_LENGTH('hello') | CHAR_LENGTH('我们') |
+------------------+----------------------+---------------------+
| 65 | 5 | 2 |
+------------------+----------------------+---------------------+
1 row in set (0.00 sec)
SELECT LENGTH('hello'),LENGTH('我们')
FROM DUAL;
#一个汉字占3个字节
#输出
+-----------------+----------------+
| LENGTH('hello') | LENGTH('我们') |
+-----------------+----------------+
| 5 | 4 |
+-----------------+----------------+
1 row in set (0.00 sec)
#CONCAT:字符串拼接
# xxx worked for yyy
SELECT CONCAT(emp.last_name,' worked for ',mgr.last_name) "details"
FROM employees emp JOIN employees mgr
WHERE emp.`manager_id` = mgr.employee_id;
#CONCAT_WS:用第一个参数分隔连接后面的字符串
SELECT CONCAT_WS('-','hello','world','hello','beijing')
FROM DUAL;
#字符串的索引是从1开始的!(Java从0开始的)
#INSERT(str, idx, len,replacestr)
#将字符串str从第idx位置开始,len个字符长的子串替换为字符串replacestr
SELECT INSERT('helloworld',2,3,'aaaaa'),REPLACE('hello','lol','mmm'),REPLACE('hello','lo','mmm')
FROM DUAL;
#输出
+-------------------------------+------------------------------+---------------------------+
|INSERT('helloworld',2,3,'aaaaa')|REPLACE('hello','lol','mmm') | REPLACE('hello','lo','mmm') |
+-------------------------------+-----------------------------+-----------------------------+
| haaaaaoworld | hello | helmmm |
+--------------------------------+----------------------------+-----------------------------+
1 row in set (0.00 sec)
#大小写转换
SELECT UPPER('HelLo'),LOWER('HelLo')
FROM DUAL;
#输出
+----------------+----------------+
| UPPER('HelLo') | LOWER('HelLo') |
+----------------+----------------+
| HELLO | hello |
+----------------+----------------+
1 row in set (0.00 sec)
SELECT last_name,salary
FROM employees
WHERE LOWER(last_name) = 'King';
#严格说应该查不到-->但是Mysql大小写不严格
#输出
+-----------+----------+
| last_name | salary |
+-----------+----------+
| King | 24000.00 |
| King | 10000.00 |
+-----------+----------+
2 rows in set (0.00 sec)
SELECT LEFT('hello',2),RIGHT('hello',3),RIGHT('hello',13)
FROM DUAL;
#输出
+-----------------+------------------+-------------------+
| LEFT('hello',2) | RIGHT('hello',3) | RIGHT('hello',13) |
+-----------------+------------------+-------------------+
| he | llo | hello |
+-----------------+------------------+-------------------+
1 row in set (0.00 sec)
# LPAD:实现右对齐效果
# RPAD:实现左对齐效果
SELECT employee_id,last_name,LPAD(salary,10,'*'),LPAD(salary,10,' ')
FROM employees;
SELECT employee_id,last_name,RPAD(salary,10,'*'),RPAD(salary,10,' ')
FROM employees;
#TRIM去掉首尾空格
#LTRIM去掉左侧空格
#TRIM('oo' FROM 'ooheolloo')去掉'oo'
SELECT CONCAT('---',LTRIM(' h el lo '),'***'),
TRIM('oo' FROM 'ooheolloo')
FROM DUAL;
#REPEAT(str,n):重复n次str
#SPACE(n):提供n个空格
#STRCMP:比较字符串大小
SELECT REPEAT('hello',4),LENGTH(SPACE(5)),STRCMP('abc','abe')
FROM DUAL;
#输出
+----------------------+------------------+---------------------+
| REPEAT('hello',4) | LENGTH(SPACE(5)) | STRCMP('abc','abe') |
+----------------------+------------------+---------------------+
| hellohellohellohello | 5 | -1 |
+----------------------+------------------+---------------------+
1 row in set (0.00 sec)
#SUBSTR(str,i,len):截取str中i处起len个字符
#LOCATE('ll','hello')定位‘ll’首次出现的位置
SELECT SUBSTR('hello',2,2),LOCATE('ll','hello'),LOCATE('lll','hello')
FROM DUAL;
#输出
+---------------------+----------------------+-----------------------+
| SUBSTR('hello',2,2) | LOCATE('ll','hello') | LOCATE('lll','hello') |
+---------------------+----------------------+-----------------------+
| el | 3 | 0 |
+---------------------+----------------------+-----------------------+
1 row in set (0.00 sec)
#ELT:返回指定位置的字符串
#FIELD(s,s1,...):返回s在字符列表中首次出现的位置
#FIND_IN_SET(s1,s2):返回s1在s2中首次出现的位置
SELECT ELT(2,'a','b','c','d'),FIELD('mm','gg','jj','mm','dd','mm'),
FIND_IN_SET('mm','gg,mm,jj,dd,mm,gg')
FROM DUAL;
#输出
+------------------------+--------------------------------------+---------------------------------------+
| ELT(2,'a','b','c','d') | FIELD('mm','gg','jj','mm','dd','mm') | FIND_IN_SET('mm','gg,mm,jj,dd,mm,gg') |
+------------------------+--------------------------------------+---------------------------------------+
| b | 3 | 2 |
+------------------------+--------------------------------------+---------------------------------------+
1 row in set (0.01 sec)
#NULLIF(s1,s2):字符串s1和s2相等返回NULL,不相等返回s1
SELECT employee_id,NULLIF(LENGTH(first_name),LENGTH(last_name)) "compare"
FROM employees;
4.日期和时间函数
4.1 获取日期、时间
函数 | 用法 |
---|---|
CURDATE() ,CURRENT_DATE() | 返回当前日期,只包含年、月、日 |
CURTIME(), CURRENT_TIME() | 返回当前时间,只包含时、分、秒 |
NOW() / SYSDATE()/ CURRENT_TIMESTAMP() /LOCALTIME()/LOCALTIMESTAMP() | 返回当前系统日期和时间 |
UTC_DATE() | 返回UTC(世界标准时间) |
UTC_TIME() | 返回UTC(世界标准时间)时间 |
举例:
SELECT CURDATE(),CURRENT_DATE(),CURTIME(),CURRENT_TIME(),
NOW(),SYSDATE(),UTC_DATE(),UTC_TIME()
FROM DUAL;
#输出
+------------+----------------+-----------+----------------+---------------------+---------------------+------------+------------+
| CURDATE() | CURRENT_DATE() | CURTIME() | CURRENT_TIME() | NOW() | SYSDATE() | UTC_DATE() | UTC_TIME() |
+------------+----------------+-----------+----------------+---------------------+---------------------+------------+------------+
| 2023-05-02 | 2023-05-02 | 21:42:09 | 21:42:09 | 2023-05-02 21:42:09 | 2023-05-02 21:42:09 | 2023-05-02 | 13:42:09 |
+------------+----------------+-----------+----------------+---------------------+---------------------+------------+------------+
1 row in set (0.00 sec)
SELECT CURDATE(),CURDATE() + 0,CURTIME() + 0,NOW() + 0
FROM DUAL;
#输出
+------------+---------------+---------------+----------------+
| CURDATE() | CURDATE() + 0 | CURTIME() + 0 | NOW() + 0 |
+------------+---------------+---------------+----------------+
| 2023-05-02 | 20230502 | 211337 | 20230502211337 |
+------------+---------------+---------------+----------------+
1 row in set (0.00 sec)
4.2日期与时间戳的转换
函数 | 用法 |
---|---|
UNIX_TIMESTAMP() | 以UNIX时间戳的形式返回当前时间。SELECT UNIX_TIMESTAMP() ->1634348884 |
UNIX_TIMESTAMP(date) | 将时间date以UNIX时间戳的形式返回。 |
FROM_UNIXTIME(timestamp) | 将UNIX时间戳的时间转换为普通格式的时间 |
举例:
#日期与时间戳的转换
#UNIX_TIMESTAMP(): 以UNIX时间戳(毫秒数)的形式返回当前时间
SELECT UNIX_TIMESTAMP(),UNIX_TIMESTAMP('2023-05-02 21:00:00'),
FROM_UNIXTIME(1635173853),FROM_UNIXTIME(1683032400)
FROM DUAL;
#输出
+------------------+---------------------------------------+---------------------------+---------------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP('2023-05-02 21:00:00') | FROM_UNIXTIME(1635173853) | FROM_UNIXTIME(1683032400) |
+------------------+---------------------------------------+---------------------------+---------------------------+
| 1683035302 | 1683032400 | 2021-10-25 22:57:33 | 2023-05-02 21:00:00 |
+------------------+---------------------------------------+---------------------------+---------------------------+
1 row in set (0.00 sec)
4.3 获取月份、星期、星期数、天数等函数
函数 | 用法 |
---|---|
YEAR(date)/MONTH(date) / DAY(date) | 返回具体的日期值 |
HOUR(time)/ MINUTE(time) /SECOND(time) | 返回具体的时间值 |
MONTHNAME(date) | 返回月份:January,… |
DAYNAME(date) | 返回星期几: MONDAY,TUESDAY…SUNDAY |
WEEKDAY(date) | 返回周几,注意,周1是0,周2是1,。。。周日是6 |
QUARTER(date) | 返回日期对应的季度,范围为1~4 |
WEEK(date),WEEKOFYEAR(date) | 返回一年中的第几周 |
DAYOFYEAR(date) | 返回日期是一年中的第几天 |
DAYOFMONTH(date) | 返回日期位于所在月份的第几天 |
DAYOFWEEK(date) | 返回周几,注意:周日是1,周一是2,。。。周六是7 |
举例:
#获取月份、星期、星期数、天数等函数
SELECT YEAR(CURDATE()),MONTH(CURDATE()),DAY(CURDATE()),
HOUR(CURTIME()),MINUTE(NOW()),SECOND(SYSDATE())
FROM DUAL;
#输出
+-----------------+------------------+----------------+-----------------+---------------+-------------------+
| YEAR(CURDATE()) | MONTH(CURDATE()) | DAY(CURDATE()) | HOUR(CURTIME()) | MINUTE(NOW()) | SECOND(SYSDATE()) |
+-----------------+------------------+----------------+-----------------+---------------+-------------------+
| 2023 | 5 | 2 | 21 | 15 | 58 |
+-----------------+------------------+----------------+-----------------+---------------+-------------------+
1 row in set (0.00 sec)
#MONTHNAME(date) 返回月份:January,..
#DAYNAME(date) 返回星期几:MONDAY,TUESDAY.....SUNDAY
SELECT MONTHNAME('2021-10-26'),DAYNAME('2021-10-26'),WEEKDAY('2021-10-26'),
QUARTER(CURDATE()),WEEK(CURDATE()),DAYOFYEAR(NOW()),
DAYOFMONTH(NOW()),DAYOFWEEK(NOW())
FROM DUAL;
#输出
+-------------------------+-----------------------+-----------------------+--------------------+-----------------+------------------+-------------------+------------------+
| MONTHNAME('2021-10-26') | DAYNAME('2021-10-26') | WEEKDAY('2021-10-26') | QUARTER(CURDATE()) | WEEK(CURDATE()) | DAYOFYEAR(NOW()) | DAYOFMONTH(NOW()) | DAYOFWEEK(NOW()) |
+-------------------------+-----------------------+-----------------------+--------------------+-----------------+------------------+-------------------+------------------+
| October | Tuesday | 1 | 2 | 18 | 122 | 2 | 3 |
+-------------------------+-----------------------+-----------------------+--------------------+-----------------+------------------+-------------------+------------------+
1 row in set (0.01 sec)
4.4 日期的操作函数
函数 | 用法 |
---|---|
EXTRACT(type FROM date) | 返回指定日期中特定的部分,type指定返回的值 |
EXTRACT(type FROM date)函数中type的取值与含义:
举例:
# EXTRACT(type FROM date) 返回指定日期中特定的部分,type指定返回的值
SELECT EXTRACT(SECOND FROM NOW()),EXTRACT(DAY FROM NOW()),
EXTRACT(HOUR_MINUTE FROM NOW()),EXTRACT(QUARTER FROM '2021-05-12')
FROM DUAL;
#输出
+--------------------------+-----------------------+-------------------------------+------------------------------------+
|EXTRACT(SECOND FROM NOW())|EXTRACT(DAY FROM NOW())|EXTRACT(HOUR_MINUTE FROM NOW())| EXTRACT(QUARTER FROM '2021-05-12') |
+--------------------------+-----------------------+-------------------------------+-----------------------------------+
| 23 | 2 | 2121| 2|
+--------------------------+-----------------------+-------------------------------+------------------------------------+
1 row in set (0.00 sec)
4.5 时间和秒钟转换的函数
函数 | 用法 |
---|---|
TIME_TO_SEC(time) | 将time转化为秒并返回结果值。转化的公式为:小时3600+分钟60+秒 |
SEC_TO_TIME(seconds) | 将seconds描述转化为包含小时、分钟和秒的时间 |
举例:
SELECT TIME_TO_SEC(CURTIME()),
SEC_TO_TIME(83355)
FROM DUAL;
#输出
+------------------------+--------------------+
| TIME_TO_SEC(CURTIME()) | SEC_TO_TIME(83355) |
+------------------------+--------------------+
| 77267 | 23:09:15 |
+------------------------+--------------------+
1 row in set (0.01 sec)