当前位置:网站首页>MySQL queries the latest qualified data rows
MySQL queries the latest qualified data rows
2022-07-05 17:12:00 【1024 questions】
Do business at ordinary times , It is often the latest data that needs to be checked .
As for the latest concept , For products , What I often say is Time sequence , The latest is Recent meaning .
With examples :
This is a record sheet for recording the visits of personnel .
The data in the data sheet accurately records the color of the hat everyone wears when visiting 、 Time 、 Personnel code ( Everyone only ).
Sample data :
What needs to be done is :
Take out the latest visit records that meet the conditions .
What would you do best ?
First realize a little , Take out A101 This person is encoded Latest visit records .
First show the wrong sql Example : Take it for granted max() function .
SELECT MAX(id) AS id ,user_code,cap_color,create_time FROM vist_record WHERE user_code='A101' ;
Query results ( The wrong result ):
Obviously, at first glance, the data are similar , But it's actually wrong .
Why is it wrong , You can talk about it a little , Since someone in the comment area is interested ( Welcome brothers to express their views ).
A brief account , max It's an aggregate function , Our error example doesn't match group by To use the , At this time, in fact mysql This guy can let us execute , Many databases report errors directly .
Then execution is execution , In fact, this time mysql It is equivalent to treating the entire table as a content block for a compressed search .
We added where Conditions user_code='A101', So the whole content block does filter out other non user_code='A101' The data of .
That is to say, in the case of lax implementation ,mysql Guarantee max return ( Related column ) The maximum of , Other column fields are not guaranteed .
The correct data is :
Is that max(id) I can't use it ?
Proper use ( Will meet the conditions of the maximum id Value as a condition ):
SELECT id,user_code,cap_color,create_timeFROM vist_recordWHERE id IN (SELECT MAX(id) AS id FROM vist_record WHERE user_code='A101' )
Query results :
But see the above method of using subqueries , Everyone must have secretly cursed their mother in their hearts , It's so troublesome to get the latest data ?
Do you have something simpler ?
Yes .
for instance , We have made sure that , id Since the increase ,id The biggest data ( Eligible data ) It's the latest .
Then we can use reverse order DESC To get the latest data :
DESC That is to say In reverse order / Descending .
PS:
Use reverse order to find :
SELECT *FROM vist_recordWHERE user_code='A101'ORDER BY id DESCLIMIT 1;
Query results :
Or in reverse chronological order :
SELECT *FROM vist_recordWHERE user_code='A101'ORDER BY create_time DESCLIMIT 1;
Query results :
Is it so simple to implement ?
Then if what we need is not to specify A101 What we need is the latest data of everyone involved ?
That is, there are multiple groups .
The latest qualified data of each category
The orange box is A101 、B202 、 C303 The latest record of each , We need to take it out .
The wrong sample :
SELECT MAX(id) AS id ,user_code,cap_color,create_time FROM vist_record GROUP BY user_code
Wrong filter results :
Code correctly :
SELECT id ,user_code,cap_color,create_time FROM vist_record WHERE id in(SELECT MAX(id) AS id FROM vist_record GROUP BY user_code )
This is about MySql This is the article about querying the latest data rows that meet the conditions , More about MySql To query the latest data line content, please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- 【729. 我的日程安排表 I】
- China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
- 微信公众号网页授权登录实现起来如此简单
- 什么是ROM
- 国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
- C#实现水晶报表绑定数据并实现打印3-二维码条形码
- 【剑指 Offer】66. 构建乘积数组
- It is forbidden to copy content JS code on the website page
- Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
- 采用药丸屏的iPhone14或引发中国消费者的热烈抢购
猜你喜欢
浏览器渲染原理以及重排与重绘
Etcd build a highly available etcd cluster
Judge whether a number is a prime number (prime number)
Jarvis OJ Telnet Protocol
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
thinkphp模板的使用
Games101 notes (III)
项目引入jar从私服Nexus 拉去遇到的一个问题
深耕5G,芯讯通持续推动5G应用百花齐放
【729. 我的日程安排表 I】
随机推荐
Wsl2.0 installation
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?
How does the outer disk futures platform distinguish formal security?
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
【微信小程序】一文读懂小程序的生命周期和路由跳转
【beanshell】数据写入本地多种方法
Rider 设置选中单词侧边高亮,去除警告建议高亮
Jarvis OJ Telnet Protocol
Learnopongl notes (II) - Lighting
dried food! Semi supervised pre training dialogue model space
goto Statement
通过proc接口调试内核代码
[Jianzhi offer] 61 Shunzi in playing cards
Embedded-c Language-1
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
【Web攻防】WAF检测技术图谱
关于mysql中的json解析函数JSON_EXTRACT
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
Deep learning plus