当前位置:网站首页>MySql 怎么查出符合条件的最新的数据行?
MySql 怎么查出符合条件的最新的数据行?
2022-07-03 09:35:00 【小目标青年】
平时做业务,经常是需要查什么什么什么的最新的一条数据。
那至于最新这个概念, 对于产品来说,经常会说的是 时间顺序,最新也就是 最近的意思。
结合示例:
这是一张记录人员来访的记录表。
数据表里的数据准确记录了每个人来访时带的帽子颜色、时间、人员编码(每个人唯一)。
数据样例:
需要做到的是 :
拿出符合条件的最新的来访记录。
你会最怎么做?
先实现一点的, 取出 A101 这个人员编码的 最新来访记录 。
首先先展示错误的sql示例: 想当然地使用max() 函数。
SELECT MAX(id) AS id ,user_code,cap_color,create_time FROM vist_record WHERE user_code='A101' ;
查询结果(错误的结果):
显然咋一看出来的数据有模有样。
但是其实是错的,是按照条件筛选完,然后取了符合条件的最大的一个id值,单独替换了 id。
正确的数据是 :
那是不是max(id) 用不了了?
正确用法(将符合条件的最大id值作为条件):
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' )
查询结果:
但是看到上面使用子查询的这种方式,
大家心里面肯定也已经在暗暗地骂娘, 拿个最新数据这么麻烦?
有没有简单一点的?
有。
比如说,我们已经确定了, id是自增的,id最大的数据(符合条件的数据) 就是最新的。
那么我们就可以使用倒序 DESC 来取最新数据:
DESC 也就是 倒序/降序 。
PS:
使用倒序查找:
SELECT *
FROM vist_record
WHERE user_code='A101'
ORDER BY id DESC
LIMIT 1;
查询结果:
或者根据时间倒序:
SELECT *
FROM vist_record
WHERE user_code='A101'
ORDER BY create_time DESC
LIMIT 1;
查询结果:
就这么简单实现了吗?
那么我们如果需求要的不是指定A101 要的是涉及到的每一个人的最新数据呢?
也就是存在多组的概念。
每一类的符合条件的最新数据
橙色框就是 A101 、B202 、 C303 分别的最新记录 , 我们要取出来。
错误示例:
SELECT MAX(id) AS id ,user_code,cap_color,create_time FROM vist_record GROUP BY user_code
错误的筛选结果:
正确编码:
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
)
好了,该篇就先到这吧。
边栏推荐
- Iterator iterator enhances for loop
- Data classification: support vector machine
- 深度学习入门之自动求导(Pytorch)
- Leetcode刷题---704
- Ut2014 learning notes
- ThreadLocal原理及使用场景
- Leetcode刷题---75
- [ultra detailed] implement soft and hard interval SVM based on sklearn
- [roast & brain hole] Some Thoughts on the bullet screen interactive game of Wei Shu Wu Three Kingdoms when visiting station B
- 丢弃法Dropout(Pytorch)
猜你喜欢
Jetson TX2 刷机
Softmax regression (pytorch)
Tensorflow—Neural Style Transfer
6、 Data definition language of MySQL (1)
神经网络入门之模型选择(PyTorch)
2021-09-22
Weight decay (pytorch)
[SQL] an article takes you to master the operations related to query and modification of SQL database
Out of the box high color background system
Ut2013 learning notes
随机推荐
大型电商项目-环境搭建
User recommendation preference model based on attention enhanced knowledge perception
Leetcode skimming ---1385
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
A complete mall system
七、MySQL之数据定义语言(二)
Are there any other high imitation projects
Ut2013 learning notes
FileNotFoundError: Could not find module ‘... dll‘ (or one of its dependencies).
Leetcode刷题---217
The imitation of jd.com e-commerce project is coming
深度学习入门之线性代数(PyTorch)
Automatic derivation of introduction to deep learning (pytoch)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
Matrix calculation of Neural Network Introduction (pytoch)
Linear regression of introduction to deep learning (pytorch)
[combinatorial mathematics] pigeon's nest principle (simple form of pigeon's nest principle | simple form examples of pigeon's nest principle 1, 2, 3)
Leetcode skimming ---367
The story of a 30-year-old tester struggling, even lying flat is extravagant
Ut2015 learning notes