选择三个表并计数

时间:2017-01-01 20:51:21

标签: mysql sql

我有三个表,我需要选择电子邮件并计算表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之间的关系)

  ID | email_id  | username_id
  1  |  1        | 11
  2  |  1        | 22
  3  |  2        | 33

期待结果:

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

但它没有用,我完全错了。我该怎么办?

1 个答案:

答案 0 :(得分:1)

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

header = tableWidget.verticalHeader()
for row in range(tableWidget.rowCount()):
    print(tableWidget.item(header.logicalIndex(row), 0).text())