left join on去重
时间: 2023-09-03 22:14:32 浏览: 129
在使用left join时进行去重有多种方法可供选择。其中一种方法是使用关键字DISTINCT,例如SELECT DISTINCT a.id FROM a LEFT JOIN c ON a.id=c.a_id where c.b_id IS NOT NULL,这样可以获得a.id字段的唯一值结果,其他字段需要在内存中再次查询拼装,适用于返回单个特定值去重或返回去重后结果数量。另一种方法是使用GROUP BY进行分组,例如SELECT a.* FROM a LEFT JOIN c ON a.id=c.a_id where c.b_id IS NOT NULL GROUP BY a.id,这种方法在left join的结果基础上进行分组,直接去重,支持完美分页和查询结果,但由于GROUP BY随机返回重复行,表c中的数据无法使用,必须再次查询后内存中拼装。请根据实际场景选择适合的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [多表连接查询数据拼接、left join去重问题](https://blog.csdn.net/qq_26012495/article/details/108923936)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [从简单实例来看 left join 如何去重](https://blog.csdn.net/locahuang/article/details/125337397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文