Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择三个表并计数_Mysql_Sql - Fatal编程技术网

Mysql 选择三个表并计数

Mysql 选择三个表并计数,mysql,sql,Mysql,Sql,我有三个表,我需要选择email并计算表A和表B之间的关系,例如: 表A: ID | email 1 | test@test 2 | test2@test 3 | test3@test 表B: UID | username 11 | James 22 | Gabriel 33 | Jonas 表C:(A和B之间的关系) 预期结果: Email | Totalrelation test@test | 2 test2@tes

我有三个表,我需要选择email并计算表A和表B之间的关系,例如:

表A:

 ID   |  email
 1    |  test@test
 2    |  test2@test
 3    |  test3@test
表B:

UID   | username
11    | James
22    | Gabriel
33    | Jonas
表C:(A和B之间的关系)

预期结果:

Email      | Totalrelation
test@test  | 2
test2@test | 1
我试过:

   select tableA.email, 
    COUNT(distinct tableC.email_id) AS total from tableA as tableA, tableC as tableC GROUP BY tableC.email_id

但它不起作用,我完全错了。我该怎么做呢?

加入表格,对数据进行分组,并计算每组的数量

select a.email, count(c.id) as cnt
from tableA a
left join tableC c on c.email_id = a.id
group by a.email

我不能这样做,因为C只有电子邮件id,A有电子邮件值
select a.email, count(c.id) as cnt
from tableA a
left join tableC c on c.email_id = a.id
group by a.email