left join去重
时间: 2023-08-11 21:04:53 浏览: 124
左连接(left join)不会自动去重,它会返回左表中的所有记录,同时匹配右表中的记录。如果需要去重,可以使用group by语句结合聚合函数来实现。在左连接的结果集上使用group by可以对group的记录进行去重,并且可以计算出总分。例如,可以使用以下查询语句实现左连接去重:
```
select g.group_id, g.group_name, group_concat(m.member_name) as member_names, sum(s.score) as sum_score
from groups g
left join members m on g.group_id = m.group_id
left join score s on s.member_id = m.member_id
where m.is_quit = 0 or m.is_quit is null
group by g.group_id;
```
这个查询语句会返回每个小组的唯一记录,并且会将小组成员的姓名拼接成一个字符串,并计算出总分。
#### 引用[.reference_title]
- *1* *3* [SQL left join去重](https://blog.csdn.net/u010895119/article/details/81535540)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [多重left join去重](https://blog.csdn.net/mmdadi/article/details/53406176)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文