mysql interview questions practice questions

Topic: 1. Topic: write a query sentence, input parameter: date (date), and the output form is as follows:

Insert picture description here

Question: create table work_plan
(
worker_name varchar(10), --person
start_date date, --start date
end_date date,
--end date sign_time varchar(10) --time required to check in during the dispatch period
)

insert into work_plan values
('Zhang San','2020-01-01',null,'06:30'),
('Li Si','2020-02-01','2020-02-15',' 07:00'),
('Wang Wu','2019-12-29','2020-03-30','06:00'),
('Zhao Liu','2019-12-29',' 2020-03-30','06:00')

create table sign_log
(
worker_name varchar(10),
sign_time datetime
)
insert into sign_log values
('Zhang San','2020-02-16 04:01'),
('Zhang San','2020-02-16 05:02 '),
('Zhang San','2020-02-16 06:03'),
('Wang Five','2020-02-16 07:03'),
('Wang Five','2020-02- 16 08:03'),
('Wang Wu','2020-02-16 09:03')

1. It means that someone will work as required from the beginning of the day to the end of the day. The daily check-in time during the dispatch period must be before the "required time on duty" (including the required time, accurate to the minute), or be late.
For example:
if 7:00 is required, 6:59 or 7:00:59 will not be considered late; 7:01 will be regarded as 1 minute late

2. The "
End Date of Assignment " in line 1 is null, which means that the person's work end time has not yet been determined and is still moving bricks; the end date of assignment in line 2 is 2020-02-15, which means that the worker is dispatched on 02- Ended on the 15th.

3. Assuming that the employee’s name is not repeated, there is only one dispatch information per person. The
punch-in record sheet will generate a record
every time an employee presses his fingerprint to attend attendance. Note: 1. 2020-02-16 Li Sipai’s construction period has ended and is not in the dispatch period There is no need to calculate attendance, so it doesn’t need to display 2. Zhao Liu didn’t check in on that day, and he was counted as being late. The time of being late is 1440 minutes. Answer:




#################答案##################
SELECT a.worker_name,a.sign_time,DATE(b.sign_time) sign_date,MIN(TIME(b.sign_time)) SIGNED,
CASE WHEN b.sign_time IS NULL OR CONCAT('2020-02-16',' ',a.sign_time)<MIN(b.sign_time) 
THEN '是' ELSE '否' END isLate,
CASE WHEN b.sign_time IS NULL THEN 1440
WHEN TIMESTAMPDIFF(MINUTE,CONCAT('2020-02-16',' ',a.sign_time),MIN(b.sign_time))<=0 THEN 0
ELSE TIMESTAMPDIFF(MINUTE,CONCAT('2020-02-16',' ',a.sign_time),MIN(b.sign_time)) END late_minute
FROM work_plan a LEFT JOIN sign_log b ON a.worker_name=b.worker_name
WHERE DATE(b.sign_time)='2020-02-16' 
AND end_date IS NULL OR '2020-02-16' BETWEEN start_date AND end_date 
GROUP BY a.worker_name;
#################答案##################

Topic: 2. In addition, please simulate the following scenario to write the corresponding SQL statement:

1. One day the clock-in machine is abnormal, and the attendance of all employees of the company needs to be processed at 7:00 on that day (Note: those who have clocked in that day do not need to be processed)
Answer:

SELECT w.worker_name, 
CASE WHEN s.sign_time IS NULL THEN '7:00:00' 
	ELSE MIN(s.sign_time) END 打卡时间
FROM work_plan w
LEFT JOIN sign_log s ON w.`worker_name`=s.`worker_name`
GROUP BY w.worker_name

Guess you like

Origin blog.csdn.net/qq_42005540/article/details/114325274