我想问一下使用mysql的查询
我有一个名为video_stat的表,这里是字段
CREATE TABLE IF NOT EXISTS `video_stat` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`video_id` int(10) NOT NULL,
`member_id` int(10) NOT NULL,
`counter` int(11) NOT NULL,
`daydate` varchar(15) NOT NULL,
`monthdate` varchar(10) NOT NULL,
`epochtime` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
'counter'字段将每天更新
我的问题是,我想对此字段进行排序,将计数器总计为每个video_id的总数,然后按每个video_id的总数对它们进行排序,然后在热门视频页面上显示该列表。我怎么能这样做?
我认为使用此查询可以正常工作
SELECT SUM( counter ) AS total_viewed, member_id, video_id
FROM member_video_stat
但它只会总结计数器字段上的所有内容并返回单行,因为SUM(计数器)部分没有计入每个特定的video_id
说明:
当前表格
+--------------+-----------+---------+
| id| video_id | member_id | counter |
+---+----------+-----------+---------+
| 4 | 6 | 2 | 1 |
| 5 | 9 | 6 | 1 |
| 6 | 12 | 2 | 1 |
| 7 | 6 | 2 | 1 |
| 8 | 12 | 2 | 1 |
+--------------+-----------+---------+
类似
+----------+-----------+---------+
| video_id | member_id | total |
+----------+-----------+---------+
| 6 | 2 | 2 |
| 9 | 6 | 1 |
| 12 | 2 | 2 |
+----------+-----------+---------+
请帮忙 非常感谢
答案 0 :(得分:2)
试一试
SELECT SUM(counter) AS total_viewed, member_id, video_id
FROM member_video_stat
GROUP BY video_id
ORDER BY total_viewed DESC
安和