左连接附加信息
我有 2 个表
table1: households
Serial_no | Address |
---|---|
sn1 | New York |
sn2 | Maryland |
sn3 | France |
table2: citizens
Serial_id | Fullname | Role | family_id |
---|---|---|---|
1 | John | Head | sn1 |
2 | Jane | Spouse | sn1 |
3 | Johny | Son | sn1 |
4 | Mike | Head | sn2 |
我希望输出如下:
Serial_no | Address | Total_count | Head |
---|---|---|---|
sn1 | New York | 3 | John |
sn2 | Maryland | 1 | Mike |
sn3 | France | 0 | null |
我被困在这里了。请帮忙。提前致谢!
I have 2 tables
table1: households
Serial_no | Address |
---|---|
sn1 | New York |
sn2 | Maryland |
sn3 | France |
table2: citizens
Serial_id | Fullname | Role | household_id |
---|---|---|---|
1 | John | Head | sn1 |
2 | Jane | Spouse | sn1 |
3 | Johny | Son | sn1 |
4 | Mike | Head | sn2 |
I want the output to be like this:
Serial_no | Address | Total_count | Head |
---|---|---|---|
sn1 | New York | 3 | John |
sn2 | Maryland | 1 | Mike |
sn3 | France | 0 | null |
I'm stuck here. please help. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
group by
进行条件聚合,如下所示:架构和插入语句:
查询:
输出:
db>>小提琴 此处
You can use conditional aggregation with
group by
like below:Schema and insert statements:
Query:
Output:
db<>fiddle here