duanfei9278 2015-03-18 22:01
浏览 66
已采纳

有帖子mysql的热门用户

I have 2 tables users | posts.

The posts structure is:

[post_id | post_user_id ]

The users structure is:

[user_id | user_name| user_password ]

Now i want to get the top 5 user with posts

edit 1:

       $cod_sql = "select u.user_name, count(*) as 'TotalNumberofPosts'
from users u
  join posts p
    on u.user_id=p.post_user_id
group by user_name
order by count(*) desc 
limit 5";
        $top10 = mysql_query($cod_sql1) or die (error_sql(mysql_error(),__LINE__,__FILE__));

            echo '<div class="head_panel">Members</div> 
                    <div class="body_panel">
                <table border="1" width="100%">
                    <tr>
                    </tr>   
                    <tr class="tr3">
                Top5 : 
                    </tr>';

    if ( $top10 === FALSE ) 
    { 
        // An error has occured 
        print "There was an error running the query" . mysql_error(); 
    } else { 
        while ( $row = mysql_fetch_assoc($top10) ) 
        { 
            print "This is some data from a row<BR />"; 
            print "Name: ($row) <BR />"; 
        } 
    }  

          }
        break;
  • 写回答

2条回答 默认 最新

  • duanmengmiezen8855 2015-03-18 22:47
    关注

    The following code will calculate the total number of posts for each user and display only the top 5 posters. SQL Fiddle demonstration.

    select u.user_name, count(*) as 'TotalNumberofPosts'
    from users u
      join posts p
        on u.user_id=p.post_user_id
    group by user_name
    order by count(*) desc 
    limit 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b