MySQL 投票表,查找每个用户对某个条目最近的投票,并根据值进行计数

发布于 2024-10-18 23:09:50 字数 1094 浏览 5 评论 0原文

使用我没有创建的数据库结构,希望在简单的 MySQL 查询上获得一些帮助。我有一个投票表 votes,其中包含以下字段:

vote_id = 唯一投票标识符

上投票的条目的 ID

item_id = 在voter_id code> = 参与投票的会员id

vote_date = 投票日期

vote = 投票类型(1 或 2)

每个用户可以投票多次在 item_id 指定的网站条目上。他们可以投 1 票,即“喜欢”票,也可以投 2 票,即“不喜欢”票。他们每次投票都会创造一个新记录。我希望能够找到每个用户对特定 item_id 的最新 vote 值,然后能够实际对投票列进行求和或计数(如果投票值)是一个“like”(值为1)

例如

vote_id   item_id   voter_id   vote_date     vote
60        9         27         1273770151    1
153       9         45         1274896188    1
163       9         3          1274918584    1
164       9         3          1275021495    2
1051      9         181        1290839090    1

我想获取每个用户最新的vote值,然后计算有多少票为1。在这种情况下,# 将为 3

27  = 1
45  = 1
3   = 2
181 = 1
      3

理想情况下,一旦我为每个 item_id 获得了“分数”,查询就能够将每个项目的分数求和为所有当前“喜欢”的总数。在网站上。

感谢您对此的任何帮助。我尽最大努力通过搜索找到这个问题的答案,但没有什么是完全正确的。

非常感谢。

Working with a database structure I didn't create and hoping for some help on a simple MySQL query. I have a voting table votes with these fields:

vote_id = unique vote identifier

item_id = id of the entry voted on

voter_id = the id of the member who entered the vote

vote_date = the date of the vote

vote = the type of vote (1 or 2)

Each user can vote multiple times on a site entry specified by item_id. They can place a vote of 1 which is a "like" vote, or a vote of 2 which is an "unlike" vote. Each time they vote, a new record is created. I would like to be able to find the most recent vote value for each user on a particular item_id and then be able to actually SUM or COUNT the vote column IF the vote value is a "like" (value of 1)

For example

vote_id   item_id   voter_id   vote_date     vote
60        9         27         1273770151    1
153       9         45         1274896188    1
163       9         3          1274918584    1
164       9         3          1275021495    2
1051      9         181        1290839090    1

I want to get the newest vote values for each user, and then do a count of how many votes of 1 there are. In this case, the # would be 3

27  = 1
45  = 1
3   = 2
181 = 1
      3

Ideally once I have a "score" for each item_id, the query would be able to SUM the score of each one into a total # of ALL current "likes" on the site.

Thanks for any help with this. I did my best to find an answer to this by searching but nothing was exactly right.

Much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赠意 2024-10-25 23:09:50

[编辑:]我添加了值 (1052, 10, 3, 1290839091, 1) 以更好地解决问题,并且必须将“item_id”添加到提取最新投票的子查询中。


耶!终于有一个我可以真正回答的 stackoverflow 问题了!!!我已经浏览了一个星期寻找一些简短的内容甜蜜&在我的巷子里。感谢您提出一个有趣的 SQL 问题!

首先,您需要提取最新投票。 (voter_id, item_id, vote_date) 的组合最好是唯一的,这样才能使这种方法发挥作用!

SELECT
  voter_id, item_id, MAX(vote_date) AS vote_date
FROM
  votes
GROUP BY
  voter_id, item_id

结果:

+----------+---------+------------+
| voter_id | item_id | vote_date  |
+----------+---------+------------+
|        3 |       9 | 1275021495 |
|        3 |      10 | 1290839091 |
|       27 |       9 | 1273770151 |
|       45 |       9 | 1274896188 |
|      181 |       9 | 1290839090 |
+----------+---------+------------+

然后您需要将原始表与这些结果连接起来。子选择将完成这项工作。请注意上面的查询是如何复制和复制的。粘贴到下面的连接中,但现在它的名称为“tmp”:

SELECT
  v.*
FROM (
  SELECT
    voter_id, item_id, MAX(vote_date) AS vote_date
  FROM
    votes
  GROUP BY
    voter_id, item_id
  ) tmp
INNER JOIN
  votes v ON (     v.vote_date = tmp.vote_date
               AND v.voter_id  = tmp.voter_id
               AND v.item_id   = tmp.item_id
  )

结果:

+---------+---------+----------+------------+------+
| vote_id | item_id | voter_id | vote_date  | vote |
+---------+---------+----------+------------+------+
|      60 |       9 |       27 | 1273770151 |    1 |
|     153 |       9 |       45 | 1274896188 |    1 |
|     164 |       9 |        3 | 1275021495 |    2 |
|    1051 |       9 |      181 | 1290839090 |    1 |
|    1052 |      10 |        3 | 1290839091 |    1 |
+---------+---------+----------+------------+------+

我相信你知道从这里做什么....哦,该死,我无法控制自己,这太可爱了:

SELECT
  v.item_id, SUM(2 - v.vote) AS likes, SUM(v.vote - 1) AS dislikes
FROM (
  SELECT
    voter_id, item_id, MAX(vote_date) AS vote_date
  FROM
    votes
  GROUP BY
    voter_id, item_id
  ) tmp
INNER JOIN
  votes v ON (     v.vote_date = tmp.vote_date
               AND v.voter_id  = tmp.voter_id
               AND v.item_id   = tmp.item_id
  )
GROUP BY
  v.item_id

结果:

+---------+-------+----------+
| item_id | likes | dislikes |
+---------+-------+----------+
|       9 |     3 |        1 |
|      10 |     1 |        0 |
+---------+-------+----------+

[EDIT:] I added values (1052, 10, 3, 1290839091, 1) to better exercise the problem, and had to add "item_id" to the sub-query that extracts newest votes.


Yay! Finally a stackoverflow question I can actually answer!!! I've been browsing around for a week looking for something short & sweet & up my alley. Thanks for a fun SQL problem!

First, you need to extract the newest votes. The combination of (voter_id, item_id, vote_date) better be unique for this approach to work!

SELECT
  voter_id, item_id, MAX(vote_date) AS vote_date
FROM
  votes
GROUP BY
  voter_id, item_id

Results:

+----------+---------+------------+
| voter_id | item_id | vote_date  |
+----------+---------+------------+
|        3 |       9 | 1275021495 |
|        3 |      10 | 1290839091 |
|       27 |       9 | 1273770151 |
|       45 |       9 | 1274896188 |
|      181 |       9 | 1290839090 |
+----------+---------+------------+

And then you need to join the original table against these results. A sub-select will do the job. Notice how the query above is copy & pasted into the join below, but now it's given the name "tmp":

SELECT
  v.*
FROM (
  SELECT
    voter_id, item_id, MAX(vote_date) AS vote_date
  FROM
    votes
  GROUP BY
    voter_id, item_id
  ) tmp
INNER JOIN
  votes v ON (     v.vote_date = tmp.vote_date
               AND v.voter_id  = tmp.voter_id
               AND v.item_id   = tmp.item_id
  )

Results:

+---------+---------+----------+------------+------+
| vote_id | item_id | voter_id | vote_date  | vote |
+---------+---------+----------+------------+------+
|      60 |       9 |       27 | 1273770151 |    1 |
|     153 |       9 |       45 | 1274896188 |    1 |
|     164 |       9 |        3 | 1275021495 |    2 |
|    1051 |       9 |      181 | 1290839090 |    1 |
|    1052 |      10 |        3 | 1290839091 |    1 |
+---------+---------+----------+------------+------+

I trust you know what to do from here.... oh darn it, I can't help myself, this is too cute:

SELECT
  v.item_id, SUM(2 - v.vote) AS likes, SUM(v.vote - 1) AS dislikes
FROM (
  SELECT
    voter_id, item_id, MAX(vote_date) AS vote_date
  FROM
    votes
  GROUP BY
    voter_id, item_id
  ) tmp
INNER JOIN
  votes v ON (     v.vote_date = tmp.vote_date
               AND v.voter_id  = tmp.voter_id
               AND v.item_id   = tmp.item_id
  )
GROUP BY
  v.item_id

Results:

+---------+-------+----------+
| item_id | likes | dislikes |
+---------+-------+----------+
|       9 |     3 |        1 |
|      10 |     1 |        0 |
+---------+-------+----------+
娜些时光,永不杰束 2024-10-25 23:09:50

不确定这个问题,这是您要找的吗?

从投票中选择 COUNT(*) 个,其中 vote = '1' AND item_id = '9'

Unsure of the question, is this what you're looking for?

SELECT COUNT(*) FROM votes WHERE vote = '1' AND item_id = '9'

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