分组...(mysql加入)

时间:2013-03-15 16:33:49

标签: php mysql left-join

我不知道如何为分组汽车提供颜色。品牌(不同)......请帮忙。

我的问题不能简单解释(很多代码)所以请看:http://sqlfiddle.com/#!2/17bac6/2

我想:

brand   blue    red    green    black
volvo    320    7289    3313     1198
citroen  538    3286
seat    1245
audi    1076    2153    2595
bmv      135    5637     864      282

TNX

1 个答案:

答案 0 :(得分:0)

根据我的理解,这应该是你正在寻找的。

SELECT  cars.brand,
(
  SELECT COUNT(*) FROM blue 
  WHERE blue.prr = cars.id_p
) as "blue",
(
  SELECT COUNT(*) FROM red 
  WHERE red.prr = cars.id_p
) as "red",
(
  SELECT COUNT(*) FROM green 
  WHERE green.prr = cars.id_p
) as "green",
(
  SELECT COUNT(*) FROM black 
  WHERE black.prr = cars.id_p
) as "black"
FROM    cars
WHERE   cars.user_id = 54 
AND     cars.pend between '2012-01-01' AND '2012-12-31' 
GROUP BY cars.brand

如果您设计了该架构,请查看数据库规范化,并了解关系,因为设计不是很好(没有违法行为)