当前位置:网站首页>[MySQL] query valid data based on time
[MySQL] query valid data based on time
2022-06-27 15:00:00 【Yiyuery】
Query the current time minute expireMin To expireMinEnd Internal data
The time parameter only needs to modify the divisor immediately 60 Represents one minute 60s
SELECT
dis.*
FROM
(SELECT
*,
(
UNIX_TIMESTAMP(gmt_deadline) - UNIX_TIMESTAMP(NOW())
) / 60 AS m
FROM
T_DISPATCH) dis
LEFT JOIN T_RECV_REPORT rec
on dis.src_id = rec.rep_id
WHERE dis.m <![CDATA[ >= ]]> #{expireMin}
AND dis.m <![CDATA[ <= ]]> #{expireMinEnd}
AND dis.disp_type=2
AND dis.disp_state=0
AND rec.rep_way=1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
Query data older than seven days and set it to expired status
The time parameter only needs to modify the divisor immediately 3600*24 It means a day 24 Hours , Every hour 3600s
UPDATE
T_APOLLO_MSG_CACHE
SET
is_expired = 1
WHERE msg_id IN
(SELECT
msg.msg_id
FROM
(SELECT
*,
(
UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(gmt_update)
) / 3600 / 24 AS d
FROM
T_APOLLO_MSG_CACHE) msg
WHERE msg.is_send = 0
AND msg.recieve_user_token =#{token}
AND msg.d > #{validDay}) ;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
Query today's data
select COUNT(1)
from
T_DISPATCH
WHERE TO_DAYS(gmt_update) = TO_DAYS(NOW());
- 1.
- 2.
- 3.
- 4.
Actual case
resultMap
public class StatItem {
private String unit;
private Integer number;
public String getUnit() {
return unit;
}
public StatItem setUnit(String unit) {
this.unit = unit;
return this;
}
public Integer getNumber() {
return number;
}
public StatItem setNumber(Integer number) {
this.number = number;
return this;
}
}
<resultMap id="statItem" type="cn.showclear.utio.web.pojo.vo.StatItem">
<result column="unit" property="unit" jdbcType="VARCHAR"/>
<result column="number" property="number" jdbcType="INTEGER"/>
</resultMap>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
Query the hourly data of the current day
<!-- same day / Hours -->
<select id="statDispatchInDayWithDispType" resultMap="statItem">
SELECT
HOUR(dis.gmt_update) AS unit,
COUNT(1) AS number
FROM
T_DISPATCH dis
WHERE TO_DAYS(dis.gmt_update) = TO_DAYS(NOW())
AND dis.disp_type = #{dispType}
GROUP BY HOUR(dis.gmt_update)
ORDER BY HOUR(dis.gmt_update)
</select>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
Query daily data of the current month
<!-- The current month / Every day -->
<select id="statDispatchInMonthWithDispType" resultMap="statItem">
SELECT
COUNT(1) AS number,
DATE_FORMAT(dis.gmt_update, '%Y-%m-%d') AS unit
FROM
T_DISPATCH dis
WHERE DATE_FORMAT(dis.gmt_update, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
AND dis.disp_type = #{dispType}
GROUP BY DATE_FORMAT(dis.gmt_update, '%Y-%m-%d')
</select>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Query the data of each month in the current year
<!-- The current year / monthly -->
<select id="statDispatchInYearWithDispType" resultMap="statItem">
SELECT
COUNT(1) AS number,
DATE_FORMAT(dis.gmt_update, '%Y-%m') AS unit
FROM
T_DISPATCH dis
WHERE DATE_FORMAT(dis.gmt_update, '%Y') = DATE_FORMAT(CURDATE(), '%Y')
AND dis.disp_type = #{dispType}
GROUP BY MONTH(dis.gmt_update)
</select>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Reference resources :
1、mysql Query daily, weekly, monthly and yearly data methods
2、 mysql Query today 、 yesterday 、 Last month, 、 This month's data
3、mysql Query today 、 yesterday 、7 God 、 near 30 God 、 This month, 、 Last month data
边栏推荐
- Openssf security plan: SBOM will drive software supply chain security
- Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
- Brief reading of dynamic networks and conditional computing papers and code collection
- Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?
- 跨境电商多商户系统怎么选
- About the meaning of the first two $symbols of SAP ui5 parameter $$updategroupid
- 老师能给我说一下固收+产品主要投资于哪些方面?
- 直播app运营模式有哪几种,我们该选择什么样的模式?
- my. INI file configuration
- External memory
猜你喜欢
机械硬盘和ssd固态硬盘的原理对比分析

Reflection learning summary

Programming skills: script scheduling

Too many requests at once, and the database is in danger

AutoCAD - line width setting

How QT sets some areas to be transparent in the background image

Integration of entry-level SSM framework based on XML configuration file

Synchronized and lock escalation

HTTP Caching Protocol practice

CAS之比较并交换
随机推荐
522. 最长特殊序列 II / 剑指 Offer II 101. 分割等和子集
Too many requests at once, and the database is in danger
优雅的自定义 ThreadPoolExecutor 线程池
ThreadLocal之强、弱、软、虚引用
Resolve activity startup - lifecycle Perspective
Web chat room system based on SSM
Library management system
2022-2-16 learning the imitated Niuke project - Section 6 adding comments
Integration of entry-level SSM framework based on XML configuration file
Cannot determine value type from string ‘<p>1</p>‘
[business security-04] universal user name and universal password experiment
Nvidia Deepstream 运行延迟,卡顿,死机处理办法
AbortController的使用
2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
Hyperledger Fabric 2. X custom smart contract
Gaode map IP positioning 2.0 backup
Sword finger offer II 039 Histogram maximum rectangular area monotonic stack
阅读别人的代码,是一种怎样的体验
[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
Getting to know cloud native security for the first time: the best guarantee in the cloud Era