如何计算此MySQL查询中的每个结果?

时间:2012-06-03 18:56:12

标签: php mysql count

我有一个表格,其中每一行都会跟踪点击链接的详细信息。此查询显示用户点击链接时前5个页面:

$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个最常点击的网址列表。如何在此查询中添加点击次数?

类似的东西:

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

2 个答案:

答案 0 :(得分: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>";

}

答案 1 :(得分: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>";

}