sql count(*)查询执行多个结果的另一个查询的结果

时间:2018-05-08 05:32:27

标签: mysql sql hibernate join count

如何对执行多个结果的另一个查询的结果执行sql count(*)查询?

我有3张桌子: -

  1. user_subscribe表我在哪里' user_id'和其他专栏。

  2. 用户表我们有' user_id' &安培; ' COUNTRY_ID' user_id与user_subscribe表链接的列和country_id正在与' country'表

    1. 有country_id'的国家/地区表列。
  3. 我需要根据属于同一个国家/地区的用户(user_id)获取user_subscribe的总数。 我非常感谢帮助,因为我在过去7天内遇到了这个问题。

2 个答案:

答案 0 :(得分:2)

SELECT COUNT(US.user_id) as country_user_subscribed
FROM user_subscribe as US
LEFT JOIN users as U ON user.id=US.user_id
LEFT JOIN countries as C on C.id=U.country_id
GROUP BY C.country_id

答案 1 :(得分:1)

尝试此解决方案:

SELECT C.CountryName,Count(u.UserId) AS UserCount FROM  User_Subscribe AS us 
LEFT JOIN User AS u ON us.UserId = u.UserId
LEFT JOIN Country AS c ON u.CountryId = c.CountryId
WHERE c.CountryId = 3
GROUP BY c.CountryName