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
                )
相关推荐
后端小肥肠1 分钟前
GitHub星标20K!Chat2DB:用说人话的方式写SQL
数据库·sql·openai
jay丿15 分钟前
在 Django 中通过 `/media/xxxx` URL 访问上传资源的安全性与实践
数据库·django·sqlite
黑色幽默灬19 分钟前
【docker】部署MySQL容器
mysql·docker·容器
安於宿命20 分钟前
【MySQL】表的约束
android·mysql·性能优化
互联网上的猪25 分钟前
MySQL 进阶学习笔记(包括MySQL的存储引擎、索引、SQL优化、视图、存储过程、触发器、锁InnoDB引擎和MySQL管理)的相关内容详细版
笔记·学习·mysql
QQ_7781329741 小时前
一文掌握 PostgreSQL 的各种指令(PostgreSQL指令备忘)
数据库
SelectDB2 小时前
天翼云:Apache Doris + Iceberg 超大规模湖仓一体实践
大数据·数据库·数据分析
__淡墨青衫__2 小时前
Django之旅:第二节--启动运行django
数据库·django·sqlite
是阿建吖!2 小时前
【MySQL】数据库基础
数据库·mysql
hrrrrb2 小时前
【MySQL】多表操作 —— 外键约束
android·数据库·mysql