我有一个名为baskets的表,其中包含以下列:
使用Query1,我确定每个篮子中的水果总数,并且还包括每个篮子的颜色:
SELECT basket, colour, apples+bananas+oranges+pears+peaches AS fruit
FROM baskets;
Query1由三列组成:
使用Query2,我通过从Query1的结果中提取信息来确定每种颜色的所有篮子中的平均水果数量:
SELECT DISTINCT
candidate.colour,
candidate.fruit
(SELECT AVG(fruit)
FROM Query1 AS average
WHERE average.colour = candidate.colour) AS fruit
FROM Query1 AS candidate;
Query2由两列组成:
是否可以嵌套这些查询,以便我只用一个查询获得Query2的结果?
非常感谢您的帮助。谢谢。
答案 0 :(得分:2)
SELECT colour, AVG(apples+bananas+oranges+pears+peaches) AS fruit
FROM baskets
GROUP by colour;
答案 1 :(得分:1)
如果你想要一篮子颜色的总水果,你会做这样的事情:
SELECT colour, SUM(apples+bananas+oranges+pears+peaches) AS totalfruit
FROM baskets
GROUP By colour