有SQL文如下所示:
SELECT place_id, content_type, id, size
FROM (SELECT id, content_type, place_id, size FROM contents
WHERE place_id IN
(SELECT DISTINCT place_id FROM place_publications
WHERE created_at <= '2010-10-02 00:00:00')) subset;
查询结果为:
[img]http://dl.iteye.com/upload/attachment/333242/2d0fadf9-67fd-326b-9e8a-645267237512.jpg[/img]
需求是统计每个place中各个种类的文件个数(file_num),文件总SIZE数(file_size)。
此时需要利用GROUP BY来实现,分别按照place_id和content_type来GROUP BY即可统计出所需的数据。
实现该需求的SQL文为:
SELECT place_id, content_type, COUNT(id) file_num, SUM(size) file_size
FROM (SELECT id, content_type, place_id, size FROM contents
WHERE place_id IN
(SELECT DISTINCT place_id FROM place_publications
WHERE created_at <= '2010-10-02 00:00:00')) subset
[color=red]GROUP BY place_id,content_type;[/color]
统计结果是:
[img]http://dl.iteye.com/upload/attachment/333246/da02a94e-9da8-39e6-a1c2-7b3d8ad52600.jpg[/img]
SELECT place_id, content_type, id, size
FROM (SELECT id, content_type, place_id, size FROM contents
WHERE place_id IN
(SELECT DISTINCT place_id FROM place_publications
WHERE created_at <= '2010-10-02 00:00:00')) subset;
查询结果为:
[img]http://dl.iteye.com/upload/attachment/333242/2d0fadf9-67fd-326b-9e8a-645267237512.jpg[/img]
需求是统计每个place中各个种类的文件个数(file_num),文件总SIZE数(file_size)。
此时需要利用GROUP BY来实现,分别按照place_id和content_type来GROUP BY即可统计出所需的数据。
实现该需求的SQL文为:
SELECT place_id, content_type, COUNT(id) file_num, SUM(size) file_size
FROM (SELECT id, content_type, place_id, size FROM contents
WHERE place_id IN
(SELECT DISTINCT place_id FROM place_publications
WHERE created_at <= '2010-10-02 00:00:00')) subset
[color=red]GROUP BY place_id,content_type;[/color]
统计结果是:
[img]http://dl.iteye.com/upload/attachment/333246/da02a94e-9da8-39e6-a1c2-7b3d8ad52600.jpg[/img]