我有一个user_id键,显示在表A ,表B ,表C 。
表A
User_id value
1 A
2 B
3 C
表B
User_id value
1 A
1 B
1 C
表C
User_id value
1 A
1 B
0 C
tA在 User_id 上是唯一的。我试图在一个查询中使用该user_id获取表B和表C中出现的次数。
我试过了:
SELECT tA.user_id, (SELECT count(*) FROM tB as countB GROUP BY tA.user_id),(SELECT count(*) FROM tC as countC GROUP BY tA.user_id) FROM tA where tA.user_id='1'
答案 0 :(得分:0)
我认为这就是你要做的事情:
select ta.user_id,
(select count(*) from tb where ta.user_id = tb.user_id) CountB,
(select count(*) from tc where ta.user_id = tc.user_id) CountC
from ta
where ta.user_id = 1
答案 1 :(得分:0)
您是否尝试过使用count(distinct)?
SELECT tA.user_id, count(distinct tB.id), count(distinct tC.id) FROM tA, tB, tC
WHERE tA.user_id=tB.user_id AND tA.user_id=tC.user_id AND tA.user_id='1';
编辑:假设你在tB和tC上有一个唯一的字段。