我正在尝试弄清楚如何在下面的while循环中显示用户名列,以及计算每个用户有多少次点击。
$query = "SELECT company, COUNT(company) FROM tableName GROUP BY company";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['COUNT(company)'] ."
". $row['company'] ." hits total so far";
echo "<br />";
}
例如
There are 3 facebook hits total so far - user1(1 total), user2(1 total), user3(1 total)
There are 2 google hits total - user1(2 total)
There are 6 mircosoft hits total - user3(1 total)
There are 0 dropbox hits total
感谢您提供的任何帮助!
添
答案 0 :(得分:2)
您想要的基本数据来自:
SELECT company, userid, COUNT(*)
FROM tableName
GROUP BY company, userid;
您想要的格式近似为:
SELECT company, GROUP_CONCAT(userid, '(', cnt, ' total)')
FROM (SELECT company, userid, COUNT(*) as cnt
FROM tableName
GROUP BY company, userid
) cu
GROUP BY company;
当然,这对“dropbox”没有一行。对于该功能,您需要一个包含感兴趣公司的表格,您需要LEFT JOIN
来查询此信息。
正如其他人可能会注意到的那样,“mysql”已经过时,你应该使用mysqli或PDO。