Subselect不遵守GROUP BY

时间:2012-06-22 16:37:22

标签: mysql sql

尝试过这种查询的方式有多种,我似乎无法正确地选择“count_lonely_vehicles”。 'count_lonely_vehicles'子选择目前只计算一个vehicle_id关联的照片总数,但不像其他SELECT那样对经销商提供GROUP BY。

dealerships (has many vehicles)
-----------
id | name

vehicles (has many photos and belongs to dealership)
--------
id | dealership_id

photos (belongs to vehicle)
------
id | vehicle_id

SELECT DISTINCT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    (
        SELECT COUNT(*)
        FROM (
            SELECT COUNT(photos.vehicle_id) AS 'count'
            FROM photos
            GROUP BY photos.vehicle_id
            HAVING COUNT(photos.vehicle_id) = 1
        ) AS tbl
    ) AS 'count_lonely_vehicles'
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
GROUP BY dealerships.id
ORDER BY count_photos DESC

因此,当在模板中循环经销商数组并吐出'count_lonely_vehicles'时,它总是相同的数字。预期的行为是收集每个经销商只有一个vehicle_id关联的照片记录数。

如何重写此查询以实现此目的?

编辑:已回答

这是在所选答案的帮助下最终起作用的查询。还必须修复一些在所选答案中被误解的字段名称。

SELECT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(DISTINCT photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    l.num_lonely
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
LEFT OUTER JOIN (
    SELECT
        v.dealership_id,
        COUNT(*) AS num_lonely
    FROM dealerships d
    JOIN vehicles v ON v.dealership_id = d.id
    JOIN (
        SELECT p.vehicle_id
        FROM photos p
        GROUP BY p.vehicle_id
        HAVING COUNT(*) = 1
    ) p ON p.vehicle_id = v.id
    GROUP BY d.id
) l ON vehicles.dealership_id = l.dealership_id

GROUP BY dealerships.id
ORDER BY count_photos DESC

2 个答案:

答案 0 :(得分:4)

您需要加入经销商信息才能获得您想要的产品。我更喜欢将子查询移动到“FROM”子句中来执行此操作:

FROM dealerships JOIN
     vehicles
     ON vehicles.dealership_id = dealerships.id JOIN
     photos ON photos.vehicle_id = vehicles.id left outer join
     (select d.dealership_id, count(*) as num_lonely
      from dealerships d JOIN
           vehicles v
           ON v.dealership_id = d.id JOIN
           (select p.vehicle_id
            from photos p
            group by p.vehicle_id
            having count(*) = 1
           ) p
           ON p.vehicle_id = v.id
     group by d.dealership_id
     ) l
     on d.dealership_id = l.dealership_id

您还可以通过加入表并执行相关子查询来修复它。

答案 1 :(得分:1)

您需要将主经销商ID值输入您的子查询中。试试这个:

SELECT DISTINCT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    (
        SELECT COUNT(*)
        FROM (
            SELECT COUNT(photo2.vehicle_id) AS 'count'
            FROM photos photo2
            JOIN vehicles vehicle2 on photo2.vehicle_id = vehicle2.vehicle_id 
            WHERE vehicle2.dealership_id = dealerships.dealership_id
            GROUP BY photo2.vehicle_id
            HAVING COUNT(photo2.vehicle_id) = 1
        ) AS tbl
    ) AS 'count_lonely_vehicles'
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
GROUP BY dealerships.id
ORDER BY count_photos DESC