当前位置:网站首页>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 .
边栏推荐
- Leetcode刷题---189
- How to hide cvxpy warnings: warn: a- > P (column pointers) not strictly increasing, column x empty?
- If you always feel that you need to persist in learning English
- Iterator iterator enhances for loop
- MySQL checks for automatic updates at 0:00 every day
- 深度学习入门之线性回归(PyTorch)
- Ut2016 learning notes
- Leetcode skimming ---263
- Tensorflow—Neural Style Transfer
- Flink--自定义函数
猜你喜欢

Numpy quick start (II) -- Introduction to array (creation of array + basic operation of array)

神经网络入门之预备知识(PyTorch)

神经网络入门之模型选择(PyTorch)

C project - dormitory management system (1)

Classification (data consolidation and grouping aggregation)

ThreadLocal原理及使用场景

Iterator iterator enhances for loop
![[SQL] an article takes you to master the operations related to query and modification of SQL database](/img/d7/7ac7788a586c4b9c0d7cdf54d974eb.png)
[SQL] an article takes you to master the operations related to query and modification of SQL database

Ut2013 learning notes

A detailed explanation of vector derivative and matrix derivative
随机推荐
Classification (data consolidation and grouping aggregation)
How to hide cvxpy warnings: warn: a- > P (column pointers) not strictly increasing, column x empty?
如何监测服务器主机的进出流量?
Hou Jie -- STL source code analysis notes
Introduction to deep learning linear algebra (pytorch)
Softmax 回归(PyTorch)
ThreadLocal principle and usage scenario
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Data captured
Bidding website architecture project progress -- Network Security
Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
Type de contenu « Application / X - www - form - urlencoded; Charset = utf - 8 'not supported
Flink--Chain的条件源码分析
正常一英寸25.4厘米,在影像领域是16厘米
mysql5.7安装和配置教程(图文超详细版)
QT:QSS自定义QToolButton实例
Practical part: conversion of Oracle Database Standard Edition (SE) to Enterprise Edition (EE)
Pytoch has been installed, but vs code still displays no module named 'torch‘
Pour vous amener dans le monde des bases de données natives du cloud
Unity learning notes: online game pixel Adventure 1 learning process & error correction experience
