MySQL join two tables, find max count and order by

前端 未结 2 1425
再見小時候
再見小時候 2021-01-27 01:27

I am rather newbie in SQL and before this moment I only used simple queries, but now I have a problem. I have two tables. First is rating:

id  u         


        
相关标签:
2条回答
  • 2021-01-27 02:03

    You need to do something like this:

    SELECT count(id), rating.userid
    FROM rating
    JOIN daybook ON daybook.userid = rating.userid
    GROUP BY userid
    
    0 讨论(0)
  • 2021-01-27 02:12
    select daybook.userid, count(*) as count 
        from daybook, rating 
        where daybook.userid = rating.userid
        group by daybook.userid
        order by count desc
    

    But you dont even really need the daybook table:

    select userid, count(*) as count
       from rating
       group by userid
       order by count desc
    
    0 讨论(0)
提交回复
热议问题