2012-06-03 83 views
0

我有一張表,其中每行跟蹤有關鏈接何時被點擊的詳細信息。該查詢顯示,前5頁,用戶是在當他們點擊鏈接:如何在此MySQL查詢中獲得每個結果的計數?

$pop_pages = ("SELECT url FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5"); 

foreach($pop_pages as $page) { 

$page_string = $page_string."<li>".$page->url."</li>"; 

} 

這將使前5名最頻繁點擊的URL列表。如何在此查詢中添加點擊次數的次數?

喜歡的東西:

$page_string = $page_string."<li>".$page->url." (X clicks)</li>"; 
+0

請完善你的問題實在是很難理解。什麼是你的查詢中的url和鏈接的區別? – nischayn22

回答

0

試試這個:

$pop_pages = ("SELECT url, count(url) as c FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5"); 

foreach($pop_pages as $page) { 

$page_string = $page_string."<li>".$page->url." (".$page->c." clicks)</li>"; 

} 
+0

謝謝,這就是我一直在尋找的。 – JROB

0

你可以做這樣的事情:

$pop_pages = ("SELECT url, COUNT(*) as count FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5"); 

foreach($pop_pages as $page) { 

    $page_string = $page_string."<li>".$page->url." (".$page->count." clicks)</li>"; 

} 
相關問題