当前位置:网站首页>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 .
边栏推荐
- Promoted, colleagues become subordinates and don't cooperate with work
- Ind wks first week
- Automatic derivation of introduction to deep learning (pytoch)
- C#项目-寝室管理系统(1)
- 2021-09-22
- Free online markdown to write a good resume
- 帶你走進雲原生數據庫界扛把子Amazon Aurora
- A complete answer sheet recognition system
- Set ArrayList nested map set loop traversal
- Leetcode刷题---283
猜你喜欢

Matrix calculation of Neural Network Introduction (pytoch)

User recommendation preference model based on attention enhanced knowledge perception

Hou Jie -- STL source code analysis notes

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

Ut2012 learning notes

Model selection for neural network introduction (pytorch)

EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS

深度学习入门之线性回归(PyTorch)

MySQL checks for automatic updates at 0:00 every day

神经网络入门之矩阵计算(Pytorch)
随机推荐
CSDN, I'm coming!
Ind kwf first week
Seata分布式事务失效,不生效(事务不回滚)的常见场景
Jetson TX2 brush machine
Ind wks first week
多层感知机(PyTorch)
[untitled]
Leetcode skimming ---278
Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档
Detailed cross validation and grid search -- sklearn implementation
Wechat applet training notes 1
Automatic derivation of introduction to deep learning (pytoch)
如何在游戏中制作一个血条
Leetcode刷题---1
Promoted, colleagues become subordinates and don't cooperate with work
Flink--自定义函数
带你走进云原生数据库界扛把子Amazon Aurora
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
Leetcode skimming ---217
[untitled]
