我的sql字段总和使用另一个字段来区分然后排序

发布于 2024-09-25 11:56:39 字数 1461 浏览 3 评论 0原文

我想问一些关于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2024-10-02 11:56:39

尝试一下

SELECT SUM(counter) AS total_viewed, member_id, video_id
FROM member_video_stat
GROUP BY video_id
ORDER BY total_viewed DESC

这个

Give this a try

SELECT SUM(counter) AS total_viewed, member_id, video_id
FROM member_video_stat
GROUP BY video_id
ORDER BY total_viewed DESC

Hoa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文