当前位置:网站首页>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 !


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051641542423.html