dongliao9018 2015-06-05 01:53
浏览 71
已采纳

php显示COUNT的结果? 我认为

I'm trying to figure out how to display the username column in the while loop below and also with counting how many hits each user had.

 $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 />";

 }

enter image description here

For example

 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

I appreciate any help you can provide!

Tim

  • 写回答

1条回答 默认 最新

  • douju1953 2015-06-05 01:57
    关注

    The basic data you want comes from:

    SELECT company, userid, COUNT(*)
    FROM tableName
    GROUP BY company, userid;
    

    An approximation of the format you want is:

    SELECT company, GROUP_CONCAT(userid, '(', cnt, ' total)')
    FROM (SELECT company, userid, COUNT(*) as cnt
          FROM tableName
          GROUP BY company, userid
         ) cu
    GROUP BY company;
    

    Of course, this does not have a row for "dropbox". For that functionality, you need a table with the companies of interest and you need to LEFT JOIN it to this query.

    As others may note, "mysql" is outdated, you should use mysqli or PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行