MySQL Multiple Counts in Single Query

前端 未结 1 1271
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 03:46

I am trying to COUNT two columns in a single query, but the results are spitting out the same values for medcount and uploadcount. Any suggestions?

SELECT *         


        
相关标签:
1条回答
  • 2020-12-21 04:17

    Use:

       SELECT tm.*, 
              x.listcount, 
              y.uploadcount 
         FROM $tbl_members tm 
    LEFT JOIN (SELECT tl.username,
                      COUNT(tl.listname) AS listcount
                 FROM $tbl_list tl
             GROUP BY tl.username) x ON x.username = tm.username
    LEFT JOIN (SELECT tu.username,
                      COUNT(tu.id) AS uploadcount
                 FROM $tbl_uploads tu
             GROUP BY tu.username) y ON y.username = tm.username
     GROUP BY tm.username 
     ORDER BY tm.lastname, tm.firstname
    
    0 讨论(0)
提交回复
热议问题