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

n53p2ov0  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(430)

如何对另一个执行多个结果的查询的结果执行sql count(*)查询?
我有三张table:——
user\u subscribe表,其中有“user\u id”和其他列。
用户表中有“user\u id”和“country\u id”列,其中user\u id与user\u subscribe表链接,country\u id与“country”表链接。
具有“country\u id”列的country表。
我需要得到总计数的用户订阅基于用户(用户id)谁属于同一个国家。一个帮助是非常感谢的,因为我被困在这个问题从过去7天。

uxhixvfz

uxhixvfz1#

尝试此解决方案:

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
xmq68pz9

xmq68pz92#

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

相关问题