MySQL ~ 取得網頁總瀏覽人數

我的寫法是配合子查詢的.........不知道有沒有更好的寫法= ="

這個是取得當日瀏覽數的語法
SELECT COUNT(DISTINCT(`ip`)) as xx FROM `popular` WHERE DATE(`date`) = DATE(NOW())

改成這樣~~~她會把每天~~~當日的總瀏覽數變成xx欄位~~~ 一個一個輸出
SELECT COUNT(DISTINCT(`ip`)) as xx FROM `popular` GROUP BY DATE(`date`)

再把它變成子查詢~~~~外面用SUM函式把xx加總起來
SELECT SUM(xx) FROM (SELECT COUNT(DISTINCT(`ip`)) as xx, date FROM `popular` GROUP BY DATE(`date`) ) as xx

跑出來的東西就是總瀏覽數~~~~

可以參考MySQL的當日瀏覽數做法XD
http://bittyferrari.blogspot.com/2008/07/mysql.html

沒有留言: