SQL 多变关联使用子查询去重

不去重状态

复制代码
select a.*,
       b.recon_amt
from free_settlement_first a
         left join free_settlement_second b on a.settlement_first_id = b.settlement_first_id

有2条数据出现了重复

使用子查询去重

复制代码
select a.*,
       b.recon_amt
from free_settlement_first a
         left join free_settlement_second b on a.settlement_first_id = b.settlement_first_id
where not exists(select 1
                 from free_settlement_second b2
                 where a.settlement_first_id = b2.settlement_first_id
                   and b.settlement_second_id > b2.settlement_second_id
                )
相关推荐
DashVector9 分钟前
如何通过Java SDK分组检索Doc
java·数据库·面试
Olrookie43 分钟前
XXL-JOB GLUE模式动态数据源实践:Spring AOP + MyBatis 解耦多库查询
java·数据库·spring boot
苏婳66644 分钟前
【最新版】怎么下载mysqlclient并成功安装?
数据库·python·mysql
Tapdata3 小时前
《实时分析市场报告 2025》上线 | 从批处理到实时洞察,2025 年全球实时分析市场全景解读
数据库
海梨花3 小时前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存
Java水解3 小时前
MySQL 亿级数据表平滑分表实践:基于时间分片的架构演进
后端·mysql
代码的余温4 小时前
SQL性能优化全攻略
数据库·mysql·性能优化
回家路上绕了弯6 小时前
MySQL 详细使用指南:从入门到精通
java·mysql
MaxHua6 小时前
SQL语法大全指南:从基础到进阶的关键字与用法解析
后端·mysql
回家路上绕了弯6 小时前
MySQL 索引详解:从原理到最佳实践
后端·mysql