1

我的数据库结构包含:

行动

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| placement_id | int(11)      | YES  | MUL | NULL    |                |
| lead_id      | int(11)      | NO   | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

展示位置

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| publisher_id | int(11)      | YES  | MUL | NULL    |                |
| name         | varchar(255) | NO   |     | NULL    |                |
| status       | tinyint(1)   | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

线索

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| status  | varchar(255) | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

我想为每个展示位置(分组)检索多个状态:

| placement_id | placement_name | count
+--------------+----------------+-------
| 123          | PlacementOne   | 12
| 567          | PlacementTwo   | 15

我尝试了无数次,我写的每一个查询都看起来很愚蠢,所以我什至不会在这里发布它们。我没希望了。一个很好评论的查询(所以我可以学习)将不胜感激。

4

1 回答 1

5
select a.placement_id, p.name as placement_name, count(l.status)
from actions a
inner join placements p on p.id = a.placement_id 
inner join leads l on l.id = a.lead_id 
group by a.placement_id, p.name
于 2013-07-31T07:48:29.613 回答