水果 | 水果 |
---|---|
黄瓜 | 苹果 |
苹果 | 芒果 |
橙子 | 葡萄 |
葡萄 | 苹果 |
我的输出需要是总水果的数量。
apple:3,
Mango :1,
grape:2,
Cucumber:1,
orange:1.
这是我尝试过的不正确的方法,该怎么做?任何想法?
select s.fruit,fruit, count(*)
from grocery
group by s.fruit,fruit
水果 | 水果 |
---|---|
黄瓜 | 苹果 |
苹果 | 芒果 |
橙子 | 葡萄 |
葡萄 | 苹果 |
我的输出需要是总水果的数量。
apple:3,
Mango :1,
grape:2,
Cucumber:1,
orange:1.
这是我尝试过的不正确的方法,该怎么做?任何想法?
select s.fruit,fruit, count(*)
from grocery
group by s.fruit,fruit
select fru,count(*) from (
select `s.fruit` as fru
from grocery
union all
select fruit as fru
from grocery
)x
group by fru
我会尝试这样的事情:
SELECT fr, COUNT(*) AS total
FROM (
SELECT `s.fruit` AS fr FROM grocery
UNION ALL
SELECT fruit AS fr FROM grocery
) t
GROUP BY fr
您可以在解决方案中使用子查询并与“union all”结合使用。