当前位置:网站首页>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_codeWrong 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 !
边栏推荐
- 网站页面禁止复制内容 JS代码
- Copy mode DMA
- Embedded-c Language-2
- Error in compiling libssh2. OpenSSL cannot be found
- C# TCP如何限制单个客户端的访问流量
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 【微信小程序】一文读懂小程序的生命周期和路由跳转
- easyNmon使用汇总
- C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
- 【剑指 Offer】62. 圆圈中最后剩下的数字
猜你喜欢

Jarvis OJ Flag

The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale

Rider 设置选中单词侧边高亮,去除警告建议高亮

Games101 notes (II)

Deep learning plus

PHP talent recruitment system development source code recruitment website source code secondary development

CMake教程Step1(基本起点)

Machine learning compilation lesson 2: tensor program abstraction

Application of threshold homomorphic encryption in privacy Computing: Interpretation

Jarvis OJ Telnet Protocol
随机推荐
CMake教程Step6(添加自定义命令和生成文件)
CMake教程Step1(基本起点)
Embedded-c language-6
Rider 设置选中单词侧边高亮,去除警告建议高亮
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
Embedded-c Language-3
【微信小程序】一文读懂小程序的生命周期和路由跳转
CMake教程Step2(添加库)
【729. 我的日程安排表 I】
CMake教程Step3(添加库的使用要求)
MYSQL group by 有哪些注意事项
一文了解MySQL事务隔离级别
Embedded-c Language-2
CMake教程Step4(安装和测试)
Jarvis OJ Telnet Protocol
thinkphp模板的使用
【性能测试】全链路压测
启牛商学院股票开户安全吗?靠谱吗?
Detailed explanation of printf() and scanf() functions of C language
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!