当前位置:网站首页>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 !
边栏推荐
- 张平安:加快云上数字创新,共建产业智慧生态
- PHP talent recruitment system development source code recruitment website source code secondary development
- 调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
- 干货!半监督预训练对话模型 SPACE
- [Jianzhi offer] 61 Shunzi in playing cards
- Machine learning compilation lesson 2: tensor program abstraction
- 一文了解MySQL事务隔离级别
- 通过proc接口调试内核代码
- 美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
- Judge whether a number is a prime number (prime number)
猜你喜欢

thinkphp3.2.3

Deep learning plus

Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.

Error in composer installation: no composer lock file present.

ECU introduction

Embedded UC (UNIX System Advanced Programming) -3

Learnopongl notes (I)

Etcd 构建高可用Etcd集群

采用药丸屏的iPhone14或引发中国消费者的热烈抢购

stirring! 2022 open atom global open source summit registration is hot!
随机推荐
【Web攻防】WAF检测技术图谱
一文了解MySQL事务隔离级别
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
CMake教程Step4(安装和测试)
[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
[Jianzhi offer] 62 The last remaining number in the circle
[729. My Schedule i]
C how TCP restricts the access traffic of a single client
【剑指 Offer】63. 股票的最大利润
Wsl2.0 installation
What is ROM
Games101 notes (I)
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
CMake教程Step3(添加库的使用要求)
C# TCP如何设置心跳数据包,才显得优雅呢?
关于mysql中的json解析函数JSON_EXTRACT
mysql中取出json字段的小技巧
composer安装报错:No composer.lock file present.
Copy mode DMA
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem