I am trying to count a distinct value in either of two rows. For instance, a table with columns fruit0, fruit1. I can get a count of the distinct values of either row, but I want a count of them combined (note this is a stupid contrived example).
我正在尝试在两行中计算一个不同的值。例如,一个带有列的表,结果是0,结果是1。我可以得到任意一行的不同值的计数,但我希望它们的计数组合在一起(注意这是一个愚蠢的设计示例)。
Example:
例子:
id | fruit0 | fruit1
--------------------
0 | apple | banana
1 | apple | pear
2 | apple | apple
3 | pear | banana
I want something like:
我希望类似:
fruit | count
--------------
apple | 4
banana| 2
pear | 2
1 个解决方案
#1
5
select fruit_name, count(*)
FROM
(
SELECT fruit0 as fruit_name
FROM table1
UNION ALL
SELECT fruit1 as fruit_name
FROM table1
)aaa
GROUP BY fruit_name
#1
5
select fruit_name, count(*)
FROM
(
SELECT fruit0 as fruit_name
FROM table1
UNION ALL
SELECT fruit1 as fruit_name
FROM table1
)aaa
GROUP BY fruit_name