SQL中是否有办法计算表中两个字段的不同组合的出现次数,例如
categorynum itemnum
1 3
2 1
1 3
1 2
3 1
1 3
并在计算(1; 3)的出现时返回3?
答案 0 :(得分:4)
当然,只需使用常规GROUP BY
/ COUNT(*)
SELECT categorynum, itemnum, COUNT(*) occurrences
FROM {table}
GROUP BY categorynum, itemnum
如果您想要特定组合,只需添加WHERE
子句(GROUP BY
之前):
WHERE categorynum = 1 AND itemnum = 3