Article directory
Functions in MySQL are mainly divided into the following four categories: string functions, numeric functions, date functions, and process functions
MySQL functions are internal functions provided by the MySQL database. These internal functions can help users process the data in the table more conveniently
The MySQL function can process the data in the table accordingly, so as to obtain the data that the user wants. These functions can make the MySQL database more powerful
1. String functions
Due to changes in business requirements, the job number is unified to 6 digits, and all those with less than 5 digits are filled with 0 in front. For example: The job number of employee No. 1 should be 0000a1
update customer set cust_id = lpad(cust_id, 6, '0')
select * from customer
2. Numerical functions
round: rounded
select round(6.66666666,6) '四舍五入';
Generate a random six-digit verification code
Idea: The random number can be obtained through the rand() function, but the obtained random number is between 0 and 1, so you can multiply it by 1000000, and then discard the decimal part. If the length is less than 6 digits, add 0
select lpad(round(rand()*1000000 , 0), 6, '0')
3. Date function
1. curdate: current date
select curdate();
2. curtime: current time
select curtime();
3. now: current date and time
select now();
4. YEAR , MONTH , DAY: current year, month, day
select YEAR(now());
select MONTH(now());
select DAY(now());
select quarter(curdate()) AS '当前季度'
select weekofyear(curdate()) AS '当前周数'
select YEAR(now()) '年',MONTH(now())'月',DAY(now())'日';
Get year month day
Get current quarter
Get current week number
5. date_add: increase the specified time interval
select date_add(now(), INTERVAL 70 YEAR );
6. datediff: Get the number of days between two dates
select datediff('2021-10-01', '2021-12-01');
6.1 Query the entry days of all employees and sort them in reverse order according to the entry days
Idea: The number of days of entry is the current date - entry date, so you need to use the datediff function to complete.
SELECT NAME AS '姓名',
datediff( curdate(), entrydate ) AS 'entrydays'
FROM
emp
ORDER BY
entrydays DESC;
6.2 Understand the total number of users who have practiced the questions and the total number of practiced questions in August 2021
In August 2021, just match the date field. There are three main matching methods:
(1)like语法:date like “2021-08%”
(2)year、month函数:year(date)=‘2021’ and month(date)=‘08’;
(3)date_format函数:date_format(date, ‘%Y-%m’)=‘2021-08’;
select
count(distinct device_id)did_cnt,
count(question_id)question_cnt
from question_practice_detail
where date_format(date,'%Y-%m')= '2021-08'
# where date like '%2021-08%'
Calculate the number of user practice questions per day in August 2021.
The key is: use the date function to extract each day in August, day(date) day
select
day(date) day,
count(question_id) question_cnt
from question_practice_detail
where date like '%2021-08%'
group by day
7. Get the number of days between two dates
SELECT DATEDIFF('2023-09-06','2023-01-06')
4. Process function
case when then else end
Requirements: Query the employee name and work address of the emp table (Beijing/Shanghai ----> first-tier cities, others ----> second-tier cities)
SELECT NAME AS '姓名',
( CASE workaddress WHEN '北京' THEN '一线城市' WHEN '上海' THEN '一线城市' ELSE '二线城市' END ) AS '工作地址'
FROM
emp;
SELECT
id,
NAME,
( CASE WHEN math >= 85 THEN '优秀' WHEN math >= 60 THEN '及格' ELSE '不及格' END ) '数学',
( CASE WHEN english >= 85 THEN '优秀' WHEN english >= 60 THEN '及格' ELSE '不及格' END ) '英语',
( CASE WHEN chinese >= 85 THEN '优秀' WHEN chinese >= 60 THEN '及格' ELSE '不及格' END ) '语文'
FROM
score;
Five, simulate the required SQL statement
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`id` int(11) NULL DEFAULT NULL COMMENT '编号',
`workno` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '工号',
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名',
`gender` char(1) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '性别',
`age` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '年龄',
`idcard` char(18) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '身份证号',
`workaddress` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '工作地址',
`entrydate` date NULL DEFAULT NULL COMMENT '入职时间'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '员工表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');
INSERT INTO `emp` VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');
INSERT INTO `emp` VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');
INSERT INTO `emp` VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');
INSERT INTO `emp` VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');
INSERT INTO `emp` VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');
INSERT INTO `emp` VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');
INSERT INTO `emp` VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');
INSERT INTO `emp` VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');
INSERT INTO `emp` VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');
INSERT INTO `emp` VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');
INSERT INTO `emp` VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');
INSERT INTO `emp` VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');
INSERT INTO `emp` VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');
INSERT INTO `emp` VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');
INSERT INTO `emp` VALUES (16, '00016', '周芷若', '女', 18, NULL, '北京', '2012-06-01');
SET FOREIGN_KEY_CHECKS = 1;