当前位置:网站首页>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 !
边栏推荐
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
- 国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
- dried food! Semi supervised pre training dialogue model space
- Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?
- Learnopongl notes (I)
- 手机开证券账户安全吗?怎么买股票详细步骤
- PHP talent recruitment system development source code recruitment website source code secondary development
- Error in composer installation: no composer lock file present.
- 齐宣王典故
猜你喜欢

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

Deep learning plus

干货!半监督预训练对话模型 SPACE

阈值同态加密在隐私计算中的应用:解读

Embedded UC (UNIX System Advanced Programming) -3

国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
MYSQL group by 有哪些注意事项

7.Scala类

Machine learning compilation lesson 2: tensor program abstraction

How to write a full score project document | acquisition technology
随机推荐
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
手机开证券账户安全吗?怎么买股票详细步骤
composer安装报错:No composer.lock file present.
Embedded -arm (bare board development) -2
Embedded -arm (bare board development) -1
The third lesson of EasyX learning
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
goto Statement
关于mysql中的json解析函数JSON_EXTRACT
Practical example of propeller easydl: automatic scratch recognition of industrial parts
【性能测试】全链路压测
Zhang Ping'an: accélérer l'innovation numérique dans le cloud et construire conjointement un écosystème industriel intelligent
Learnopongl notes (I)
Jarvis OJ webshell analysis
[Jianzhi offer] 63 Maximum profit of stock
NPM installation
一个满分的项目文档是如何书写的|得物技术
干货!半监督预训练对话模型 SPACE
Embedded-c Language-3
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields