0
SELECT 
    `list_type`.`type`
FROM
    `events`
        INNER JOIN
    `list_type` ON `events`.`type` = `list_type`.`id`
WHERE
    `events`.`date` BETWEEN '2012-01-01' AND '2012-12-31'
        AND `events`.`center_id` = 2
GROUP BY `events`.`name`

Baseline
Baseline
Baseline
Baseline
Baseline
Baseline
Baseline
Special Events
Special Events
Special Events

但我希望它像这样计算每种类型

Baseline    7
Special Events  3

所以我尝试了这个

SELECT 
    `list_type`.`type`,
    COUNT(*)
FROM
    `events`
        INNER JOIN
    `list_type` ON `events`.`type` = `list_type`.`id`
WHERE
    `events`.`date` BETWEEN '2012-01-01' AND '2012-12-31'
        AND `events`.`center_id` = 2
GROUP BY `events`.`name`

但它给了我这个:

Baseline    45
Baseline    3
Baseline    56
Baseline    23
Baseline    12
Baseline    9
Baseline    2
Special Events  5
Special Events  4
Special Events  18

它正在计算每个名称和类型的数量,而不是输出表单查询。我也试过 COUNT( list_type. type) 谢谢你的帮助。

4

3 回答 3

0

GROUP然后由list_type.type

SELECT  list_type.type,
        COUNT(*) totalCount
FROM    events
        INNER JOIN list_type 
            ON events.type = list_type.id
WHERE   events.date BETWEEN '2012-01-01' AND '2012-12-31'
        AND events.center_id = 2
GROUP   BY list_type.type
于 2013-01-28T16:01:12.067 回答
0

不要分组events.name

尝试按 分组list_typetype

由于您是按事件分组的,因此它会根据事件为您提供结果和数字!

于 2013-01-28T16:01:18.603 回答
0

您始终可以使用子查询计算查询的结果,如下所示:

SELECT type, count(*)
FROM (
  ...your query above...
) s
GROUP BY type

它肯定会奏效,但不一定是最好的方法。我认为您应该按以下方式分组list_type.type

GROUP BY list_type.type

然后您必须为每种类型计算不同的事件名称,如下所示:

SELECT 
    `list_type`.`type`,
    COUNT(DISTINCT `events`.`name`)
于 2013-01-28T16:09:54.840 回答