从投票表中回显 SQL 查询
我有一个带有投票功能的图像库,我试图显示数据库中每个图像的投票数。
我有一个投票表,内容如下...
vote_id │ user_id │ session_id │ ip │ created_date │ status
我试图获取每个要显示的图像的投票数(每页几张图像),但每个 ip 只有 1 票,并且仅在上周(周一至周日)内。
对于这类事情,我不是天才,但这就是我迄今为止能够用 SQL 得到结果的方法。我在尝试将其实现到 PHP 中时遇到了一点困难...
SELECT COUNT(DISTINCT ip) AS Votes FROM 'vote'
(我知道它的基本原理,但你就知道了)
I have an image gallery with a vote function, and I'm trying to display the number of votes for each image from the database.
I have a Votes table with the following...
vote_id │ user_id │ session_id │ ip │ created_date │ status
I am trying to get the number of votes for each image to display (several images per page), but only 1 vote per ip and only within the last week (Mon to Sun).
I'm not a genius when it comes to this sort of thing, but this is what I have been able to do so far to get results in SQL. I've been getting a bit stuck trying to implement it into PHP...
SELECT COUNT(DISTINCT ip) AS Votes FROM 'vote'
(I know its basic but there you go)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是:)
<代码>
SELECT COUNT(DISTINCT ip) AS Votes, WEEK(created_date) AS week_num FROM 'vote'
按周(创建日期)分组
按 week_num DESC 排序
Here is :)
SELECT COUNT(DISTINCT ip) AS Votes, WEEK(created_date) AS week_num FROM 'vote'
group by WEEK(created_date)
order by week_num DESC
您必须对数据进行分组:
group by ip, week(created_date)
you have to group data:
group by ip, week(created_date)
投票表中应该有一个像 image_id 这样的列。然后你可以通过这个查询来完成这个任务。
SELECT image_id, COUNT(DISTINCT ip) AS Votes FROM vote GROUP BY image_id
You should have a column like image_id in the votes table. Then you can accomplish this by this query.
SELECT image_id, COUNT(DISTINCT ip) AS Votes FROM vote GROUP BY image_id