当前位置:网站首页>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 !
边栏推荐
- [Jianzhi offer] 62 The last remaining number in the circle
- NPM installation
- Error in composer installation: no composer lock file present.
- Embedded-c Language-4
- [7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
- CMake教程Step3(添加库的使用要求)
- Deep dive kotlin synergy (XXI): flow life cycle function
- 机器学习01:绪论
- Wsl2.0 installation
- 国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
猜你喜欢
随机推荐
[Web attack and Defense] WAF detection technology map
Judge whether a number is a prime number (prime number)
[first lecture on robot coordinate system]
Error in compiling libssh2. OpenSSL cannot be found
winedt常用快捷键 修改快捷键latex编译按钮
手机开证券账户安全吗?怎么买股票详细步骤
Deeply cultivate 5g, and smart core continues to promote 5g applications
CMake教程Step2(添加库)
Deep learning plus
IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
goto Statement
【二叉树】根到叶路径上的不足节点
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
Timestamp strtotime the day before or after the date
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
The second day of learning C language for Asian people
【微信小程序】一文读懂小程序的生命周期和路由跳转
Embedded-c Language-4