通过使用left join,我试图计算现有购买的形状数(如果没有购买,则为0)。我还按形状类型对它们进行分组。但我得到的结果都是0。
表格形状:
id shape colour
1 circle red
2 rectangle blue
3 triangle green
4 star yellow
5 cross purple
6 moon orange
购买桌型:
id price
3 3.24
2 1.95
3 5.42
4 6.32
5 4.21
1 7.45
3 5.97
3 4.31
1 6.32
4 7.82
SQL语言:
SELECT shapes.shape, COUNT(IF(shape_purchase.price != NULL, 1, NULL)) FROM shapes LEFT JOIN shape_purchase ON shapes.id = shape_purchase.id GROUP BY shapes.id
&这里怎么了?
结果:
shape COUNT(IF(shape_purchase.price != NULL, 1, NULL))
circle 0
rectangle 0
triangle 0
star 0
cross 0
moon 0
我叫shape_purchase.price有什么问题吗?还是左联会让事情更复杂?
仅供参考,
SELECT * FROM shapes LEFT JOIN shape_purchase ON shapes.id = shape_purchase.id
看起来像这样:
id shape colour id price
3 triangle green 3 3.24
2 rectangle blue 2 1.95
3 triangle green 3 5.42
4 star yellow 4 6.32
5 cross purple 5 4.21
1 circle red 1 7.45
3 triangle green 3 5.97
3 triangle green 3 4.31
1 circle red 1 6.32
4 star yellow 4 7.82
6 moon orange NULL NULL