我有 2 个 mysql 表,一个称为“用户”,另一个称为“连接”
表用户有关于用户的信息,并且有 3 行:
mikha
guy
maricela
表连接有 twitter 等用户之间的连接(例如 mikha 关注 maricela 和 maricela 关注 mikha)
Connections 具有以下行:
username1 | username2
--------------------------
guy | maricela
mikha | guy
mikha | maricela
我想获取有关“mikha”的信息,包括他关注的人数和关注他的人数。
我使用以下查询:
SELECT *, COUNT(DISTINCT connections.username1) AS count_following,
COUNT(DISTINCT connections.username2) AS count_followers
FROM users LEFT JOIN connections on connections.username1 = 'mikha' OR
connections.username2 = 'mikha' WHERE users.username = 'mikha'
预期的:
count_following = 2 (as mikha is following guy and maricela)
count_followers = 0 (no one following mikha yet)
实际的:
count_following = 2
count_followers = 1
谢谢问候迈克尔