当前位置:网站首页>How does MySQL find the latest data row that meets the conditions?
How does MySQL find the latest data row that meets the conditions?
2022-07-03 10:46:00 【Small target youth】
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 , Is to filter according to the conditions , Then we choose the largest one that meets the conditions id value , Replaced separately id.
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_time
FROM vist_record
WHERE 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_record
WHERE user_code='A101'
ORDER BY id DESC
LIMIT 1;
Query results :
Or in reverse chronological order :
SELECT *
FROM vist_record
WHERE user_code='A101'
ORDER BY create_time DESC
LIMIT 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
)
Okay , Let's stop here first .
边栏推荐
- Numpy quick start (I) -- pre knowledge (create array + constant + data type)
- Numpy quick start (III) -- array advanced operation
- A detailed explanation of vector derivative and matrix derivative
- FileNotFoundError: Could not find module ‘... dll‘ (or one of its dependencies).
- 神经网络入门之矩阵计算(Pytorch)
- Ind FXL first week
- Numpy quick start (II) -- Introduction to array (creation of array + basic operation of array)
- Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
- Ut2017 learning notes
- Leetcode skimming ---263
猜你喜欢
Detailed cross validation and grid search -- sklearn implementation
Weight decay (pytorch)
Classification (data consolidation and grouping aggregation)
Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档
Knowledge map enhancement recommendation based on joint non sampling learning
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
[untitled]
UI interface design related knowledge (I)
大型电商项目-环境搭建
随机推荐
Ut2016 learning notes
Leetcode刷题---374
六、MySQL之数据定义语言(一)
The story of a 30-year-old tester struggling, even lying flat is extravagant
Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得
Leetcode刷题---189
Uni app learning 1 bottom menu and parent-child components
Leetcode skimming ---367
MySQL checks for automatic updates at 0:00 every day
Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档
Type de contenu « Application / X - www - form - urlencoded; Charset = utf - 8 'not supported
Leetcode skimming ---852
【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
Automatic derivation of introduction to deep learning (pytoch)
[combinatorial mathematics] pigeon nest principle (simple form examples of pigeon nest Principle 4 and 5)
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
Jetson TX2 刷机
Classification (data consolidation and grouping aggregation)
正常一英寸25.4厘米,在影像领域是16厘米
Windows security center open blank