Question

I have a MySQL table with three columns: Page, IP and Date. I'm trying to write an SQL line that lists unique views in the last 24 hours by page name. I've tried this:

SELECT Page, COUNT(DISTINCT IP) as views FROM `hits` WHERE DATE >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND IP <> 'unknown' GROUP BY Page ORDER BY views DESC;

I do get the grouped page names. However, they all have 1 view.

Also, how can I only list pages that have more than 1 views? I tried WHERE views > 1 but it didn't work.

Était-ce utile?

La solution

Your date checking condition was wrong. You were checking for entries having dates greater than or equal to one day from now.

It should be DATE <= DATE_SUB(NOW(), INTERVAL 1 DAY)

Your query should look like this

  SELECT Page, COUNT(DISTINCT IP) as views FROM `hits` WHERE DATE <= DATE_SUB(NOW(), INTERVAL 1 DAY) AND IP <> 'unknown' GROUP BY Page ORDER BY views DESC;`
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top