Forum Moderators: coopster
I have a db table that keeps track of all visitor information. Simplified its something like this:
+-----------+------+
¦ visit id ¦ Page ¦
+-----------+------+
¦ 1 ¦ 1 ¦
¦ 2 ¦ 1 ¦
¦ 3 ¦ 2 ¦
¦ 4 ¦ 1 ¦
¦ 5 ¦ 3 ¦
¦ 6 ¦ 3 ¦
¦ 7 ¦ 3 ¦
+-----------+------+
Can anyone help me out with this?
-Thanks
GoodMoJo
SELECT DISTINCT Page, count(Page) from table_name GROUP BY Page;
How MySQL Optimizes ORDER BY [mysql.com]By default, MySQL sorts all
queries as if you specifiedGROUP BY x,y[,...]in the query as well. If you include theORDER BY x,y[,...]clause explicitly, MySQL optimizes it away without any speed penalty, though the sorting still occurs. If a query includesORDER BYbut you want to avoid the overhead of sorting the result, you can supress sorting by specifyingGROUP BY.ORDER BY NULL
ORDER BY clause just in case you ever port your code to a database that wouldn't explicity sort the result set in this manner. Therefore,
SELECT DISTINCT Page, count(Page) from table_name GROUP BY Page ORDER BY Page;
-GoodMoJo
This is the actual query I was trying to use. I wanted to add a where clause that could select the page stats for in this case january.
SELECT DISTINCT Page, count(page) FROM stats.mojows_stats GROUP BY Page FROM `stamp` > '2004-01-01 00:00:00' AND `stamp` < '2004-2-01 00:00:00' ORDER BY 2 DESC LIMIT 10
-Thanks again
SELECT [mysql.com] syntax description. Therefore, a GROUP BY clause must come after any WHERE clause. And your ORDER BY clause must come after the GROUP BY clause. I see mykel79 forgot to append that:
SELECT DISTINCT Page, count(page)
FROM stats.mojows_stats
WHERE stamp > '2004-01-01 00:00:00'
AND stamp < '2004-2-01 00:00:00'
GROUP BY Page
ORDER BY 2 DESC
LIMIT 10