计入2列

时间:2018-02-08 14:02:06

标签: sql oracle count

我需要计算在2个mysql列中找到结果的次数。

实施例

|   Name      |    Fruit    |
|   John      |    Apple    |
|   Bill      |    Banana   |
|   Sam       |    Lemon    |
|   Sam       |    Kiwi     |
|   Bill      |    Banana   |
|   John      |    Apple    |
|   Bill      |    Pear     |
|   Bill      |    Pear     |
|   Bill      |    Pear     |

我需要得到这个结果:

|   Name    |    Fruit   |   Count   | 
|   John    |    Apple   |     2     |
|   Bill    |   Banana   |     2     |
|   Bill    |    Pear    |     3     |
|   Sam     |    Kiwi    |     1     |
|   Sam     |   Lemon    |     1     |

我无法理解它如何适用于2列

1 个答案:

答案 0 :(得分:6)

您只需使用group by条款,如:

Select Name, Fruit, Count(*) as Count
from table
group by Name, Fruit