2

尝试过一百万种不同的方法后,我似乎无法正确选择“count_lonely_vehicles”。'count_lonely_vehicles' 子选择当前只计算一个车辆 ID 关联的照片总数,但不像其他选择那样对经销商.id 进行分组。

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
4

2 回答 2

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

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

于 2012-06-22T16:47:24.827 回答
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
于 2012-06-22T16:47:29.290 回答