Вопрос

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)

Это было полезно?

Решение

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

Другие советы

you have to group data:

group by ip, week(created_date)

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top