从投票表中回显 SQL 查询

发布于 2024-12-23 00:05:30 字数 375 浏览 6 评论 0原文

我有一个带有投票功能的图像库,我试图显示数据库中每个图像的投票数。

我有一个投票表,内容如下...

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 技术交流群。

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

发布评论

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

评论(3

暗恋未遂 2024-12-30 00:05:30

这是:)

<代码>
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

°如果伤别离去 2024-12-30 00:05:30

您必须对数据进行分组:

group by ip, week(created_date)

you have to group data:

group by ip, week(created_date)

一桥轻雨一伞开 2024-12-30 00:05:30

投票表中应该有一个像 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

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