我有一张如下表,
id ip等级
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中可以吗?
查询应该是这样的
SELECT id, rating, COUNT(rating) AS count
FROM yourtable
GROUP BY id,rating
ORDER BY id, rating
GROUP BY
可以一次应用于多个列。尝试:
SELECT id, rating, COUNT(id) AS count
FROM yourtable
GROUP BY id, rating
ORDER BY id, rating
SELECT `id`, `rating`, COUNT(`id`) AS `count` FROM `table` GROUP BY `id`, `rating`.
你应该更多地规范你的表。