简体   繁体   English

SQL 查询一列两列

[英]SQL query to count of two columns in one

s.fruit果子 fruit水果
Cucumber Cucumber apple苹果
apple苹果 Mango芒果
Orange橙子 grape葡萄
grape葡萄 apple苹果

My output need to be the count be of the total fruit.我的 output 需要成为总水果的数量。

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
select fru,count(*) from (
select `s.fruit` as fru
  from grocery
union all
select fruit as fru
  from grocery
)x
group by fru

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

you may use sub-query and combine with 'union all' in the solution.您可以在解决方案中使用子查询并与“union all”结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM