sql 中的函数计数

问题描述 投票:0回答:1

我有下一张桌子:

Users(ID, name, cName) (ID is a primary key)
ID - id of the user
name - name of the user
cName - name of the country where the user lives

Follows(ID1, ID2) (ID1 AND ID2 ARE PRIMARY KEYS)
id1 - id of the user that follows id2
id2 - id of the user being followed by id1

Interactions(authorID, cNum, uID, iType) (ALL OF THE VARIABLES ARE PRIMARY KEYS)
authorID - id of the user that writes content
cNum - serial number of the content
uID - id of the user that interacted with the content 
iType - type of the interaction, it can be from the type "like", "follow", "comment", "share"

我想知道数据库中每个国家至少有一名关注者的所有用户。 我编写了下一个代码,但它返回 0 数据。

SELECT u.ID, u.name
FROM Users u
JOIN Follows f ON u.ID = f.ID2
JOIN Users uf ON f.ID1 = uf.ID
GROUP BY u.ID, u.name
HAVING COUNT(DISTINCT uf.cName) = (SELECT COUNT(DISTINCT cName) FROM Users)

零不是答案。

sql mysql count pycharm user-stories
1个回答
0
投票

看起来您的查询是正确的,但有时数据可能与预期不完全一致。您的数据库中的用户可能根本没有来自每个国家/地区的关注者,或者数据中可能存在一些我们没有考虑到的细微差别。

仔细看看是否有任何差距或不一致。也许有一个特定的国家/地区的关注者数量不足,或者可能有一些用户尚未被关注。通过进一步深入研究数据,您可能会发现有助于优化查询或阐明为什么返回零结果的见解

© www.soinside.com 2019 - 2024. All rights reserved.