函数在计算机语言的使用中贯穿始终,函数的作用是什么呢?它可以把我们经常使用的代码封装起来,需要的时候直接调用即可。这样既提高了代码效率,又提高了可维护性。在SQL中我们也可以使用函数对检索出来的数据进行函数操作。使用这些函数,可以极大地提高用户对数据库的管理效率。
从函数定义的角度出发,我们可以将函数分成内置函数和自定义函数。在SQL语言中,同样也包括了内置函数和自定义函数。内置函数是系统内置的通用函数,而自定义函数是我们根据自己的需要编写的,本章及下一章讲解的是SQL的内置函数。
SELECT ABS(-123),ABS(32), SIGN(-23) ,SIGN(43),PI(),CEIL(32.32),CEILING(-43.23 ) ,FLOOR(32.32),FLOOR(-43.23 ) , MOD( 12,5)
FROM DUAL;
举例:取随机数
SELECT RAND (),RAND(),RAND (10),RAND (10),RAND(-1),RAND(-1)
FROM DUAL;
举例:四舍五入
SELECT ROUND (123.556),ROUND (123.456,0 ),ROUND(123.456,1),ROUND(123.456,2),ROUND(123.456,3)
,ROUND(123.456,-1),ROUND(123.456,-2)
FROM DUAL;
举例:截断操作
SELECT TRUNCATE(123.456,0),TRUNCATE(123.496,1),TRUNCATE(129.45,-1)
FROM DUAL;
举例:单行函数可以嵌套
注意:MySQL中,字符串的位置是从1开始的。
#获取月份、星期、星期数、天数等函数
SELECT YEAR (CURDATE()),MONTH(CURDATE()),DAY(CURDATE()),HOUR(CURTIME()),MINUTE(NOW()),
SECOND (SYSDATE())
FROM DUAL;
SELECT MONTHNAME(‘2021-18-26’),DAYNAME(‘2021-10-26’),WEEKDAY(‘2821-10-26’),QUARTER(CURDATE()),
WEEK(CURDATE()),DAYOFYEAR(NOW()),
DAYOFMONTH(NOW()),DAYOFWEEK(NOW())
FROM DUAL;
SELECT EXTRACT(SECOND FROM NOW()),EXTRACT(DAY FROM NOW()),EXTRACT(HOUR_MINUTE FROM NOW()),
EXTRACT(QUARTER FROM NOW())
FROM DUAL;
SELECT NOW(),DATE_ADD(NoW( ),INTERVAL 1 YEAR),
DATE_ADD(NoW(),INTERVAL -1 YEAR),
DATE_SUB(NoW( ),INTERVAL 1 YEAR)
FROM DUAL;
SELECT DATE_ADD(NOW(),INTERVAL 1 DAY) AS col1,
DATE_ADD(‘2022-10-04 23:32:12’,INTERVAL 1 SECOND) AS col2,
ADDDATE(‘2022-10-04 23:32:12’,INTERVAL 1 SECOND) AS col3,
DATE_ADD(‘2022-10-04 23:32:12’,INTERVAL’1_1’ MINUTE_SECOND) As col4,
DATE_ADD(NOW(),INTERVAL -1 YEAR) AS col5,#可以是负数
DATE_ADD(NOW(),INTERVAL’1_1’ YEAR_MONTH) AS col6 #需要单引号
FROM DUAL;
.SELECT ADDTIME(NOW(),20),
SUBTIME(NOW(),30),
SUBTIME(NOW(),‘1:1:3’),
DATEDIFF(NOW(),‘2821-10-01’),
TIMEDIFF(NOW(),‘2021-10-25 22:10:10’),
FROM_DAYS(366),
TO_DAYS(‘0000-12-25’),
LAST_DAY(NOW()),MAKEDATE(YEAR(NOW()),12),MAKETIME(10,21,23),PERIOD_ADD(20200101010101,10)
FROM DUAL;
SELECT DATE_FORMAT(CURDATE(),‘%Y-%M-%D’),DATE_FORMAT(NOW(),‘%Y-%m-%d’),
TIME_FORMAT(CURTIME(),‘%H:%i:%S’),
DATE_FORMAT(NOW(),‘%Y-%M-%D’ ‘%H:%i:%S %W %w %T %r’)
FROM DUAL;
SELECT STR_TO_DATE(‘2022-October-4th19:29:59 Tuesday 2 19:29:59 07:29:59 PM’,
‘%Y-%M-%D’ ‘%H:%i:%S %W %w %T %r’)
FROM DUAL;
GET_FORMATE函数中date_type和format_type参数取值如下:
SELECT GET_FORMAT(DATE, ‘USA’)
FROM DUAL;
SELECT DATE_FORMAT(CURDATE(),GET_FORMAT(DATE, ‘USA’))
FROM DUAL;
SELECT last_name, salary,IF (salary >= 6000,‘高工资’,‘低工资’) “details”
FROM employees;
SELECT last_name , commission_pct, IF (commission_pct IS NOT NULL, commission_pct,0)“details”,salary * 12*(1 + IF(commission_pct IS NOT NULL,commission_pct,0)) “annual sal”
FROM employees;
#IFNULL (VALUE1,,VALUE2):看做是IF(VALUE,VALUE1,VALUE2)的特殊情况
SELECT last_name,commission_pct,IFNULL(commission_pct,0) “details”
FROM employees;
SELECT last_name, salary,CASE WHEN salary >= 15000 THEN ‘白骨精’
WHEN salary >= 10000 THEN ‘潜力股’
WHEN salary > 8000 THEN ‘小屌丝’
ELSE ‘草根’ END “等级”
FROM employees;
/*
查询部门号为10,20,30 的员工信息,
若部门号为10,则打印其工资的1.1倍,
20号部门,则打印其工资的1.2倍,
30号部门,打印其工资的1.3 倍数,
其他部门,打印其工资的1.4倍数
*/
SELECT employee_id,last_name,department_id,salary,CASE department_id WHEN 10 THEN salary * 1.1
WHEN 20 THEN salary * 1.2 WHEN 30 THEN salary * 1.3
ELSE salary * 1.4 END “details”
FROM employees;
/*
查询部门号为10,20,30 的员工信息,
若部门号为10,则打印其工资的1.1倍,
20号部门,则打印其工资的1.2倍,
30号部门,打印其工资的1.3 倍数,
*/
SELECT employee_id,last_name,department_id,salary,CASE department_id WHEN 10 THEN salary * 1.1
WHEN 20 THEN salary * 1.2 WHEN 30 THEN salary * 1.3
END “details”
FROM employees
WHERE department_id IN (10,20,30);
注意:
PASSWORD()在mysql8.0中弃用。
ENCODE()\DECODE()在mysql8.0中弃用。
SELECT VERSION(),CONNECTION_ID(),DATABASE(),SCHEMA(),
USER(),CURRENT_USER(),CHARSET(‘XSY’),COLLATION(‘XSY’)
FROM DUAL;
如果n的值小于或者等于0,则只保留整数部分
SELECT FORMAT (123.125,2),FORMAT(123.125,0),FORMAT(123.125,-2)
FROM DUAL;
SELECT CONV(16,10,2),CONV(8888,10,16),CONV(NULL,10,2)
FROM DUAL;
以"192.168.1.100”为例,计算方式为192乘以256的3次方,加上168乘以256的2次方,加上1乘以256,再加上100。
SELECT INET_ATON(‘192.168.1.100’),INET_NTOA(3232235876)
FROM DUAL;
SELECT BENCHMARK(1000,MD5(‘mysql’))
FROM DUAL;
#utf8mb3:拿3个字节表示一个字符
#utf8mb4:拿4个字节表示一个字符
SELECT CHARSET( ‘xsy666’),CHARSET(CONVERT(‘xsy666’ USING ‘utf8mb3’))
FROM DUAL;