Mysql组由2个字段组成

时间:2014-11-16 09:18:37

标签: mysql sql

我正在尝试计算在我的数据库中找到的品牌总数

我有3张桌子

vehicle_make

+----+------+
| id | make |
+----+------+
|  1 | Audi |
|  2 | BMW  |
+----+------+

vehicle_ads

+----+-----------+----------+------+-----------+
| id | make_code |  model   | year | dealer_id |
+----+-----------+----------+------+-----------+
|  1 |         2 | 5 series | 2010 |        0  |
+----+-----------+----------+------+-----------+
|  2 |         2 | 3 series | 2014 |        20 |
+----+-----------+----------+------+-----------+

经销

+----+------------+------+
| id | dealername | make |
+----+------------+------+
| 20 | bla bla    |    2 |
+----+------------+------+

这是我的SQL查询,我尝试modify this所以我可以按make和dealer_id进行分组

SELECT j.make AS make, j.id AS id, sum(j.count) AS count FROM 
(
    (
        SELECT v.make, sum(count+dealerCount)
        FROM 
             ( SELECT a.dealer_id, v.make AS make, a.make_code, COUNT(*) AS count
                    FROM `vehicle_make` AS v
                    JOIN `vehicle_ads` AS a
                    ON (v.id=a.make_code)
                    WHERE a.dealer_id>0
                    GROUP BY a.dealer_id, a.make_code
              ) AS g
            JOIN
              ( SELECT a.dealer_id, v.make, a.make_code, COUNT(*) AS dealerCount
                    FROM `vehicle_make` AS v
                    JOIN `vehicle_ads` AS a
                    ON (v.id=a.make_code)
                    WHERE a.dealer_id>0
                    GROUP BY a.dealer_id
              ) AS gl
              ON gl.dealer_id = g.dealer_id
            JOIN
              ( SELECT a.make_code, COUNT(*) AS makeCount
                    FROM `vehicle_make` AS v
                    JOIN `vehicle_ads` AS a
                    ON (v.id=a.make_code)
                    WHERE a.dealer_id>0
                    GROUP BY a.make_code
              ) AS gg
            ON gg.make_code=g.make_code 
    )
    UNION
    (
        SELECT v.make AS make, v.id AS id, COUNT(*) AS count
                    FROM `dealership` AS d
                    JOIN `vehicle_make` AS v
                    ON (v.id=d.make)
                    GROUP BY d.make
    )
) AS j GROUP BY j.make

现在我收到了这个错误。

1052 - 字段列表中的'make'列不明确

输出应该如下所示

+--------+----+-------+
|  make  | id | count |
+--------+----+-------+
| Audi   |  1 |   300 |
| BMW    |  2 |   150 |
| Toyota |  3 |    50 |
+--------+----+-------+

基本上它应该在经销商表中获得总制造数量,并在vechile_ads表中获得总制造数量(按制造商和经销商_id分组)

知道我在这里做错了什么。

更新 SQL FIDDLE

在上面的小提琴中,NUMADS的{​​{1}}应为1(BMW分组),返回的列应为dealer_idmake,{ {1}}其中make_code为NUMADS + NUMDEALER

4 个答案:

答案 0 :(得分:1)

尝试此查询:

SELECT make.id,
       make.make,
       ( IFNULL(ads.cout, 0) + IFNULL(deal.cout, 0) ) AS cout
FROM   vehicle_make make
       LEFT OUTER JOIN (SELECT make_code                 AS id,
                               Count(DISTINCT dealer_id) AS cout
                        FROM   vehicle_ads
                        GROUP  BY make_code) ads
                    ON make.id = ads.id
       LEFT OUTER JOIN (SELECT make     AS id,
                               Count(*) AS cout
                        FROM   dealership
                        WHERE  make IS NOT NULL
                        GROUP  BY make) deal
                    ON make.id = deal.id; 

子查询ads将为count的{​​{1}} distinct dealers人提供make来自vehicle_ads表格的广告。子查询deal将为您提供dealership表中的品牌计数。最后,使用left outer join加入vehicle_make表到adsdeal,以便结果应包含所有idmake并添加计数来自两个子查询以获得所需的结果。

Sample SqlFiddle

注意:在您的sql小提琴中,将count(*)替换为count(distinct dealer_id)将为您提供所需的内容。检查here

答案 1 :(得分:0)

你想要从两个故事中获得数量。这是一种方式:

select vh.make, sum(numads) as numads, sum(numdealer) as numdealer
from ((select make_code as model_id, count(*) as numads, 0 as numdealer
       from vehicle_ads
       group by model_id
      ) union all
      (select make as model_id, 0, count(*)
       from dealership
       group by model_id
      )
     ) c join
     vehicle_make vm
     on c.model_id = vm.id
group by vh.make;

注意:您应该在数据库中一致地命名列。将make作为一个表中的字符串列,将另一个表中的整数外键引用导致混淆。我建议调用vehicle_make.id model_id之类的所有外键引用。

答案 2 :(得分:0)

SQL Fiddle是否提供了您需要的输出?

SELECT vm.`make`,
       vm.`make_code`,
       IFNULL(subq.`num_ads`, 0) AS `count`
FROM `vehicle_make` vm
LEFT JOIN
(SELECT ad.`make_code`, COUNT(*) AS `num_ads`
 FROM
 (SELECT *
  FROM `vehicle_ads`
  GROUP BY `make_code`, `dealer_id`) ad
 GROUP BY ad.`make_code`) subq
ON subq.`make_code` = vm.`id`

答案 3 :(得分:0)

您只是在连接中声明了两个名为make的列。第一个是:

SELECT a.dealer_id, v.make AS make, a.make_code, COUNT(*) AS count
FROM `vehicle_make` AS v       ^------------------------------- this field called make
JOIN `vehicle_ads` AS a
ON (v.id=a.make_code)
WHERE a.dealer_id>0
GROUP BY a.dealer_id, a.make_code

,第二个在这里:

SELECT v.make AS make, v.id AS id, COUNT(*) AS count
                   ^------------------------------------ this field also called make
FROM `dealership` AS d
JOIN `vehicle_make` AS v
ON (v.id=d.make)
GROUP BY d.make

只需重命名它们并运行你的代码..一般来说它看起来很好:)