当前位置:网站首页>MySQL中判断和向下取整的使用场景和用法
MySQL中判断和向下取整的使用场景和用法
2022-07-17 04:09:00 【Qsh.】
最近有一个需求是这样的,查询用户的请假天数和需要写日志的天数。
数据如下:
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 '请假类型 0=全天 1=上午 2=下午',
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);
');
需求一:查询请假天数
根据表中数据,我们需要查出用户id为40的用户在五月份一共请假多少天。其中类型type 0=全天(即请假一天)1=上午 2=下午
我们可以使用以下SQL查询:
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';
得到:
其中,time_long即为请假时长,半天记为0.5,一天记为1。
然后我们可以很轻松的算出该用户在该时间段内的请假时长。我们使用sum进行子查询。
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
得到:
到此时我们第一个需求就算完成啦。
需求二:查询应写日志数量
日志数量的计算方式为(以五月份为例):五月份实际天数为:31天,除去节假日和周六日外实际上班天数为:20天。这里我们可以通过节假日等API接口计算获取时长天数。
假设:我们通过接口获取到了该时间范围内的实际上班天数:20天。然后请假一天不写日志,请假半天需要写日志。
我们根据现有数据,可以得到,该用户需要写日志天数为:15天
注意:这里的5月31日合在一起请了一天假,所以5月31日不用写日志。5月20日需要写日志。
这里推荐一个节假日API:
节假日API

然后在这里我们就需要用到一个向下取整的函数 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;

反向查询得到不应该写日志的天数:5天。
最后通过代码计算得到应写日志天数:20 - 5 = 15天
以上欢迎各位点评指正。
边栏推荐
- Ftxui basic notes (botton button component Foundation)
- 基于stm32f103的智能风扇系统
- Intel + Lenovo jointly launched open source cloud solutions
- Mqant in-depth analysis
- About the problem of database, the concept of uniqueness and non repetition
- [database] must know and know at the end of the period ----- Chapter 12 database recovery
- Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
- 如何进行mysql下的严格模式修改,使得使用插入用户表方式添加新用户成功
- surging作者出具压测结果
- Simple explanation of C constructors
猜你喜欢

Build a portrait matting server based on openvino model server

Wechat e-book reading applet graduation design of applet completion works (1) development outline

Optimization and configuration of OSPF

64. 最小路径和:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。
![[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)](/img/56/f52d13f86764768eee190e22ba136a.png)
[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)

Eas (energy aware scheduling) green energy-saving scheduler

PAC Decade: witness HPC from CPU era to XPU Era

机器学习11:代价敏感学习

若依框架包名修改器

OSPF路由控制,防环相关知识
随机推荐
Common methods of C string
Brief introduction to cmtime
Vs Code common shortcut keys
ospf防环
分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
Wkwebview sets the correct posture of custom useragent
Typeorm MySQL upsert operation
Data interaction between avframe\avpacket and itself in ffmpeg
PAC Decade: witness HPC from CPU era to XPU Era
V4L2学习资料收集
[seventh issue of notebook series] download and use of openvino pre training model
Deconstruction of typescript array / object / string / function parameters
结构体通过成员变量获取主结构体地址(struct)
小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能
Introduction to Maui framework 05 MVVM data model understanding
51 single chip microcomputer to find out the input mode
OSPF comprehensive experiment
Unity - how to modify a package or localize it