当前位置:网站首页>Multiple mainstream SQL queries only take the latest one of the data
Multiple mainstream SQL queries only take the latest one of the data
2022-04-23 05:23:00 【Cx_ Xuan】
Business scenario :
ORACLE You can see :https://blog.csdn.net/qq_25223941/article/details/109358683
MYSQL You can see :https://blog.csdn.net/qq_25223941/article/details/109358496
When there are multiple pieces of data for a certain type , But I just want to take the latest one . When we can determine which type, we use simple sql You can solve it .
ORACLE-SQL:
Such as : select * from ( select * from t_table a where a.tpye=? order by a.time desc ) where rownum=1;
MYSQL-SQL:
Such as : select * from t_table a where a.type=? order by time desc limit1;
But when we're not sure which type , When full table scanning or multi data scanning is required , It needs to be used oracle The unique function in solves .
ORACLE-SQL:
Such as :select * from (select a.type,row_number() over(partition by a.type order by a.time desc) as rn from t_table a where xxxx) where rn=1;
MYSQL-SQL:
Such as :select a.type,max(id) from( select * from t_table order by time desc ) a group by a.type;
perhaps : select * from t_table order by time desc limit 1;
among partition by The following field means to distinguish and group according to this field ,order by Sort ,row_number() over This means to get the number of rows according to the conditions inside ,
Summarize the meaning of this function : according to type Field grouping by time After sorting the fields , Access to this type How much data exists in the table ( How many are there means rn How many lines )rn=1 It means that taking the first row will get the latest data .
版权声明
本文为[Cx_ Xuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220545385226.html
边栏推荐
- Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
- The concept of meta universe is popular. Is virtual real estate worth investing
- 点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
- Tensorflow realizes web face login system
- The annual transaction volume of the app store is US $1 million, and only 15% commission is paid. Small and medium-sized developers are very contradictory
- 《2021年IT行业项目管理调查报告》重磅发布!
- Musk and twitter storm drama
- Project manager's thinking mode worth trying: project success equation
- 学习笔记:Unity CustomSRP-12-HDR
- 2021-11-01
猜你喜欢
CPT 104_TTL 09
After NPM was upgraded, there was a lot of panic
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
数字化转型失败,有哪些原因?
JSP-----JSP简介
《2021年IT行业项目管理调查报告》重磅发布!
How to set the initial value of El input number to null
2021-10-08
C#测试调用PaddleSharp模块识别图片文字
Laravel implements the Holy Grail model with template inheritance
随机推荐
How to realize adaptive layout
开源规则引擎——ice:致力于解决灵活繁复的硬编码问题
Multi process model in egg -- egg document Porter
Blender程序化地形制作
Data management of basic operation of mairadb database
Top 25 Devops tools in 2021 (Part 2)
How to add beautiful code blocks in word | a very complete method to sort out and compare
[untitled]
Uniapp wechat sharing
Low code and no code considerations
Master-slave replication of MariaDB database
了解 DevOps,必读这十本书!
JS array common methods
2021-10-25
Basic knowledge of vegetable chicken database
Semi synchronous replication of MariaDB
TSlint注释忽略错误和RESTful理解
无线网怎么用手机验证码登录解决方案
使用 Kears 实现ResNet-34 CNN
(十一)vscode代码格式化配置