我是MySQL数据库的新手。我想加入两个表并获取计数。例如:
表A
_id content
1 aaa
2 bbb
3 ccc
4 ddd
表B
_id a_id b_content
1 1 aaa
2 1 bbb
3 1 ccc
4 2 ddd
5 2 eee
6 2 fff
7 3 ggg
8 3 aaa
9 3 bbb
10 3 ccc
11 3 ddd
现在我的结果表应该具有:
表C
a_id a_content count
1 aaa 3
2 bbb 3
3 ccc 5
4 ddd 0
使用联接怎么可能?
答案 0 :(得分:0)
以下查询将给出结果
SELECT a._id as a_id, a.content as a_content, (SELECT count(*) FROM table_b AS b WHERE b.a_id = a._id) as count FROM table_a AS a