作为列MYSQL的动态表计数的SUM

时间:2016-09-30 09:38:28

标签: mysql

以下mysql查询将在每个用户名后输出一列用户名和一系列5列,这些列是5个计数的结果。

SELECT f.username, 
(SELECT COUNT(id) FROM sites s WHERE s.username = f.username) site_count,
(SELECT COUNT(id) FROM banners b WHERE b.username = f.username) banner_count,
(SELECT COUNT(id) FROM favorites fa WHERE fa.username = f.username) favorite_count,
(SELECT COUNT(id) FROM social_posts so WHERE so.username = f.username) social_count,
(SELECT COUNT(id) FROM stream_updates st WHERE st.username = f.username) stream_count
FROM members f
    WHERE f.username 
      IN (SELECT friend2 as username FROM list_friends WHERE friend1 = :user
      AND friend2 <> :user)
    OR f.username 
      IN (SELECT friend1 as username FROM list_friends  WHERE friend2 = :user
    AND friend1 <> :user) 
    OR f.username = :user

示例输出:

| username | site_count | banner_count | favorite_count | social_count | stream_count |
+----------+------------+--------------+----------------+--------------+--------------+
|  user1   |     7      |       12     |        3       |       5      |     11       |
|  user2   |     2      |        1     |        12      |       1      |     0        |
|  user3   |     5      |        7     |        4       |       9      |     25       |

我希望获得的结果是每一行的总和:

| username | site_count | banner_count | favorite_count | social_count | stream_count |
+----------+------------+--------------+----------------+--------------+--------------+
|          |    14      |       20     |        19      |      15      |     36       |

2 个答案:

答案 0 :(得分:0)

只需添加一笔金额来计算?

.as-console-wrapper { max-height: 100% !important; top: 0; }

如果每个用户都需要计算,只需将SELECT f.username, SUM((SELECT COUNT(id) FROM sites s WHERE s.username = f.username)) site_count, SUM((SELECT COUNT(id) FROM banners b WHERE b.username = f.username)) banner_count, SUM((SELECT COUNT(id) FROM favorites fa WHERE fa.username = f.username)) favorite_count, SUM((SELECT COUNT(id) FROM social_posts so WHERE so.username = f.username)) social_count, SUM((SELECT COUNT(id) FROM stream_updates st WHERE st.username = f.username)) stream_count FROM members f WHERE f.username IN (SELECT friend2 as username FROM list_friends WHERE friend1 = :user AND friend2 <> :user) OR f.username IN (SELECT friend1 as username FROM list_friends WHERE friend2 = :user AND friend1 <> :user) OR f.username = :user 添加到结尾

答案 1 :(得分:0)

我的解决方案是使用用户定义的变量:=,以便使用每个总和更新变量。

有关详细信息,请参阅:http://dev.mysql.com/doc/refman/5.7/en/user-variables.html

更新查询的结束结果如下:

SELECT f.username,
@sites := SUM((SELECT COUNT(*) FROM sites s WHERE s.username = f.username)) AS site_count,
@banner := SUM((SELECT COUNT(*) FROM banners b WHERE b.username = f.username)) AS banner_count,
@favs := SUM((SELECT COUNT(*) FROM favorites fa WHERE fa.username = f.username)) AS favorite_count,
@social := SUM((SELECT COUNT(*) FROM social_posts so WHERE so.username = f.username)) AS social_count,
@stream := SUM((SELECT COUNT(*) FROM stream_updates st WHERE st.username = f.username)) AS stream_count
  FROM members f
     WHERE f.username 
     IN (SELECT friend2 as username FROM list_friends WHERE friend1 = :user
     AND friend2 <> :user)
  OR f.username 
     IN (SELECT friend1 as username FROM list_friends  WHERE friend2 = :user
     AND friend1 <> :user) 
  OR f.username = :user