需要帮助计算MySQL查询计数,如果一个特定的数字


Need help figuring out MySQL query to count if a certain number

我有一个这样的表设置:

fruit1 | fruit2 | fruit3 | fruit4 | number1 | number2 | number3 | number4
--------------------------------------------------------------------------
apple  | orange | banana |  berry |    5    |    2    |    1    |    4   
orange | banana | apple  |  berry |    3    |    2    |    5    |    2
berry  | banana | orange |  apple |    1    |    2    |    5    |    2

我需要一个MySQL查询来计算给定水果的5次次数。在这个例子中,苹果是5两次,橙子是5一次。明白了吗?水果1的号码是number1,水果2的号码是number2,等等…

我可以用一点php代码做到这一点,但我知道这是一个非常简单的MySQL查询。我只是很难把查询放在一起。

提前感谢!

SELECT fruit, COUNT(*) NumberOfInstance
FROM
(
    SELECT fruit1 fruit, number1 num FROM table1
    UNION ALL
    SELECT fruit2 fruit, number2 num FROM table1
    UNION ALL
    SELECT fruit3 fruit, number3 num FROM table1
    UNION ALL
    SELECT fruit4 fruit, number4 num FROM table1
) s
WHERE num = 5
GROUP BY fruit
    SQLFiddle Demo Link