gpt4 book ai didi

sql - 我如何在 MySQL 中统计选票?

转载 作者:可可西里 更新时间:2023-11-01 07:33:43 25 4
gpt4 key购买 nike

我有一个名为 votes 的数据库表,其中包含三列“timestamp”、“voter”和“voted_for”。

表中的每个条目代表一票。我想在某些条件下计算每个“voted_for”的所有选票。

条件如下:

  • 每个选民只能投票一次,如果一个选民投了多票,则以最近的一票为准。

  • 只计算在指定时间之前进行的投票。

最佳答案

试试这个:

SELECT voted_for, count(*)
FROM votes v
INNER JOIN (SELECT Voter, Max(timestamp) as lastTime from votes group by Voter) A
on A.Voter = v.voter and a.lasttime = v.timestamp
WHERE timestamp < {date and time of last vote allowed}
Group by voted_for

关于sql - 我如何在 MySQL 中统计选票?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4219022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com