当前位置:网站首页>Usage scenarios and usage of judgment and rounding down in MySQL
Usage scenarios and usage of judgment and rounding down in MySQL
2022-07-19 04:36:00 【Qsh.】
Recently, there is a demand like this , Query the user's leave days and the number of days to log .
The data are as follows :
DROP TABLE IF EXISTS `test2`;
CREATE TABLE `test2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL DEFAULT '0',
`date` date NOT NULL,
`type` tinyint(2) NOT NULL DEFAULT '0' COMMENT ' Leave type 0= all day 1= In the morning 2= Afternoon ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (1, 40, '2022-05-16', 0);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (2, 40, '2022-05-17', 0);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (3, 40, '2022-05-18', 0);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (4, 40, '2022-05-19', 0);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (5, 40, '2022-05-31', 2);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (6, 40, '2022-05-31', 1);
INSERT INTO `test2` (`id`, `uid`, `date`, `type`) VALUES (7, 40, '2022-05-20', 1);
');
Need one : Query leave days
According to the data in the table , We need to find out the users id by 40 How many days of leave did your users take in May . The type type 0= all day ( Ask for a day off )1= In the morning 2= Afternoon
We can use the following SQL Inquire about :
SELECT
*,
( CASE WHEN type = '0' THEN 1 WHEN type = "1" THEN 0.5 WHEN type = "2" THEN 0.5 END ) time_long
FROM
test2
WHERE
date BETWEEN '2022-05-01'
AND '2022-05-31';
obtain :
among ,time_long That is, the length of leave , Half a day is recorded as 0.5, One day is recorded as 1.
Then we can easily calculate the leave duration of the user in this time period . We use sum Make a subquery .
SELECT
SUM( time_long ) AS time_long
FROM
(
SELECT
*,
( CASE WHEN type = '0' THEN 1 WHEN type = "1" THEN 0.5 WHEN type = "2" THEN 0.5 END ) time_long
FROM
test2
WHERE
date BETWEEN '2022-05-01'
AND '2022-05-31'
) a
obtain :
By this time, our first requirement is completed .
Demand two : Query the number of logs that should be written
The number of logs is calculated as ( Take may as an example ): The actual number of days in May is :31 God , Excluding holidays, Saturdays and Sundays, the actual working days are :20 God . Here we can wait through holidays API The interface calculates the acquisition duration in days .
hypothesis : We get the actual working days in this time range through the interface :20 God . then Ask for a day off without writing a diary , Ask for leave for half a day and write a diary .
Based on the existing data , You can get , The number of days this user needs to write logs is :15 God 
Be careful : there 5 month 31 I took a day off together , therefore 5 month 31 You don't have to write a log every day .5 month 20 You need to write a log every day .
Here's a recommended one The holiday season API:
The holiday season API

Then here we need to use a function of rounding down FLOOR
SELECT
FLOOR(
SUM( time_long )) AS time_long
FROM
(
SELECT
date,
SUM( time_long ) AS time_long
FROM
(
SELECT
*,
( CASE WHEN type = '0' THEN 1 WHEN type = "1" THEN 0.5 WHEN type = "2" THEN 0.5 END ) time_long
FROM
test2
WHERE
date BETWEEN '2022-05-01'
AND '2022-05-31'
) a
GROUP BY
date
) b;

Reverse query to get the number of days that should not be written to the log :5 God .
Finally, the number of days to write logs is calculated by code :20 - 5 = 15 God
You are welcome to comment and make corrections .
边栏推荐
- Impersonate the server for requests
- 分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
- [daily question] sword finger offer II 041 Average value of sliding window
- Touchid and faceid~2
- Challenges and Countermeasures of deep forgery to national security
- Eas (energy aware scheduling) green energy-saving scheduler
- TiDB学习笔记【初阶】
- 使用小丸工具箱进行极限视频压缩
- ospf综合实验
- Real time Bi (IV) low cost data quasi real time processing idea
猜你喜欢

On the third day of security, iptables prevents nmap scanning and binlog

高仿网易云音乐UI的微信小程序源码

数据分析报告这样写,才算真正读懂了数据

HCR Huichen is walking on the north slope, a giant beast swimming into digital marketing services

安全第三天iptables防止nmap扫描以及binlog

MySQL表的查询进阶

异或和加密方式的解密的复现

T + 0 to t + 1! The quick redemption amount is reduced to 10000! Another bank adjusted the rules for the application and redemption of cash wealth management products

C语言基础犄角旮旯的知识之数据类型

SQL interface switching cannot obtain focus
随机推荐
The author of surging issued the pressure test results
nodejs-uuid
OSPF路由控制,防环相关知识
Optimization and configuration of OSPF
Android kotlin custom LinearLayout
SQL interface switching cannot obtain focus
策略模式代替if-else
[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
Cannot find module ‘process‘ or its corresponding type declarations.
On the third day of security, iptables prevents nmap scanning and binlog
JS中Class类的静态属性和静态方法
T+0变T+1!快赎金额降至1万!又有银行调整现金理财产品申赎规则
Xcode11 add a boot page (the launch images source option is missing after the upgrade)
‘ionic‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
AutoJs学习-实现极乐净土
Architecture and application of secure multiparty computing
TiDB学习笔记【初阶】
状态码含义详解
B+ tree stored procedures, triggers, substring and substr, and truncate and delete
64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m