我有一张如下表格,
id ip rating
1 192.161.0.1 3
1 192.161.0.2 4
1 192.161.0.4 3
2 192.161.0.5 1
我需要结果,比如,
id评分计数
1 3 2
1 4 1
2 1 1
可以在mysql中使用吗?
答案 0 :(得分:1)
GROUP BY
可以同时应用于多个列。尝试:
SELECT id, rating, COUNT(id) AS count
FROM yourtable
GROUP BY id, rating
ORDER BY id, rating
答案 1 :(得分:1)
SELECT `id`, `rating`, COUNT(`id`) AS `count` FROM `table` GROUP BY `id`, `rating`.
你应该更多地规范你的桌子。
答案 2 :(得分:1)
查询应该是这样的
SELECT id, rating, COUNT(rating) AS count
FROM yourtable
GROUP BY id,rating
ORDER BY id, rating