当前位置:网站首页>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
边栏推荐
- Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
- egg的static的前缀是可以修改惹,靴靴
- Error handling mechanism of the strongest egg framework in history
- 2021-11-01
- 2021-09-23
- Three 之 three.js (webgl)模型的删除/场景的清空/内存的释放 的简单整理
- Laravel database
- mariadb数据库的主从复制
- es6数组的使用
- Domain driven model DDD (III) -- using saga to manage transactions
猜你喜欢
![[untitled] kimpei kdboxpro's cool running lantern coexists with beauty and strength](/img/94/37d80984fe6fad84e73c2f8e6b2d52.jpg)
[untitled] kimpei kdboxpro's cool running lantern coexists with beauty and strength

My old programmer's perception of the dangers and opportunities of the times?

2021-10-08

Routing parameters

Detailed explanation of concurrent topics

4 个最常见的自动化测试挑战及应对措施
Basic knowledge of redis

引入精益管理方式,需要提前做到这九点

Publish your own wheel - pypi packaging upload practice

領域驅動模型DDD(三)——使用Saga管理事務
随机推荐
What are the most popular recruitment technical skills in 2022? You can't think of it
数字化转型失败,有哪些原因?
何时适合进行自动化测试?(下)
低代码和无代码的注意事项
The 2021 IT industry project management survey report was released!
Collaboration future object and concurrent futures
Uniapp hot update with progress bar
Asynchronous iterator & asynchronous generator & asynchronous context manager
学习笔记:Unity CustomSRP-10-Point and Spot Shadows
学习笔记:Unity CustomSRP-11-Post Processing---Bloom
史上最强egg框架的error处理机制
Pandas to_ SQL function pit avoidance guide "with correct code to run"
点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
Using PHP post temporary file mechanism to upload arbitrary files
Interview summary
MFC实现资源单独Dll实现
改进DevSecOps框架的 5 大关键技术
2022年最热门的招聘技术技能是什么,您绝对想不到
Tensorflow realizes web face login system
MySQL foreign key constraint