0
s.fruit fruit
Cucumber apple
apple Mango
Orange grape
grape apple

My output need to be the count be of the total fruit.

apple:3,
Mango :1,
grape:2,
Cucumber:1, 
orange:1.

This is what i tried which is not correct so how to do this ? any idea?

select s.fruit,fruit, count(*) 
from grocery
group by s.fruit,fruit
AirlineDog
  • 520
  • 8
  • 21
Ruban
  • 125
  • 7

3 Answers3

2
select fru,count(*) from (
select `s.fruit` as fru
  from grocery
union all
select fruit as fru
  from grocery
)x
group by fru
George Joseph
  • 5,842
  • 10
  • 24
2

I'd try something like this:

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
Marco
  • 56,740
  • 14
  • 129
  • 152
0

you may use sub-query and combine with 'union all' in the solution.