我的sql字段总和使用另一个字段来区分然后排序
我想问一些关于使用 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
“计数器”字段每天都会更新的
字段,我的问题是,我想通过求和来对该字段进行排序按每个 video_id 的总数进行计数,然后按每个 video_id 的总数对它们进行排序,然后在热门视频页面上显示列表。我怎样才能做到这一点?
我认为它可以使用这个查询:
SELECT SUM( counter ) AS total_viewed, member_id, video_id
FROM member_video_stat
但它只会总结计数器字段上的所有内容并返回一行,因为 SUM(counter) 部分不计算每个特定的 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 |
+----------+-----------+---------+
I would like to ask something about query using mysql
I have this table called video_stat, and here's the field
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
the 'counter' field will be updated per day
and my question is that, I would like to sort this field, by sum-ing the counter as total per video_id and then sort them by total per video_id to then display the list on a popular video page. how can I do that?
I thought that it would work using this query:
SELECT SUM( counter ) AS total_viewed, member_id, video_id
FROM member_video_stat
but it will only sums up everything on the counter field and return a single row since the SUM(counter) part did not count for each and particular video_id
illustration:
current table
+--------------+-----------+---------+
| 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 |
+--------------+-----------+---------+
to something like
+----------+-----------+---------+
| video_id | member_id | total |
+----------+-----------+---------+
| 6 | 2 | 2 |
| 9 | 6 | 1 |
| 12 | 2 | 2 |
+----------+-----------+---------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下
这个
Give this a try
Hoa