MySQL:计算某些值

时间:2011-04-05 07:07:14

标签: mysql count

选择         reporting.station     从         报告     LEFT JOIN         渠道     上         MD5(igmpurl)= reporting.station     哪里         reporting.station!= “N / A”     订购         名;

产生此表:

Query like it is

现在我想计算每个元素的数量,这应该是这样的:

Result like I'd would like to have it

注意:我知道COUNT(station)会返回行数(20),但是不是我想要的。

知道如何在MySQL(InnoDB)中解决这个问题吗? 非常感谢和问候

2 个答案:

答案 0 :(得分:3)

SELECT
    reporting.station, COUNT(reporting.station)
FROM
    reporting
LEFT JOIN
    channel
ON
    MD5(igmpurl)=reporting.station
WHERE
    reporting.station!="n/a"
GROUP BY reporting.station
ORDER BY
    name;

试试这个。

答案 1 :(得分:1)

使用COUNT()GROUP BY

在你的情况下,它应该是这样的:

SELECT
    reporting.station,
    COUNT(*) AS rep_station_count
FROM
    reporting
LEFT JOIN
    channel
ON
    MD5(igmpurl)=reporting.station
WHERE
    reporting.station!="n/a"
GROUP BY
    reporting.station
ORDER BY
    name;

请注意,在同一查询中允许普通和aggregate列(例如AVG()COUNT()等)是特定于MySQL的。