mysql需要获取其他表的依赖项计数

时间:2015-09-09 14:10:09

标签: mysql

有2个表:

`contactslistrelations`

(`id`, `contactid`, `contactlistid`)
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 4, 1),
(5, 5, 1),
(6, 6, 1),

另一个:

`contacts`

(`id`, `name`, `surname`, `email`, `unsubscribed`)
(1, "name1", "surname1", "email1", "0"),
(2, "name1", "surname2", "email1", "0"),
(3, "name1", "surname3", "email1", "0"),
(4, "name1", "surname4", "email1", "1"),
(5, "name1", "surname5", "email1", "0"),
(6, "name1", "surname6", "email1", "0"),

我需要从contactlistrelations表格中unsubscribed contacts的{​​{1}}联系人表中获取统计数据

1 个答案:

答案 0 :(得分:1)

你可以像这样使用join,这应该返回每个联系人列表中没有取消订阅的人数

Select contactlistrelations .contactlistid,count(*) AS count 
  FROM contactlistrelations 
  JOIN contacts on contacts.id = contactlistrelations.contactid 
  WHERE contacts.unsubscribed = 0
  GROUP BY contactlistrelations.contactlistid