0

我有一个类似 twitter 的网站用于学习目的,您可以在其中关注用户。现在我想获取所有关注登录人的用户。

下表有三行。id、user_id、follow_id

现在,如果用户 1 跟随用户 2。行中的值将是user_id = 1follow_id= 2

但是当我试图获取具有相同 follow_id 的用户时,我只得到 user_id 2 作为回报。并不是所有具有 follow_id=2 的用户

任何的想法 ?

SELECT 
    user.id, user.username, user.email,
    userdetails.profile_img, following.follow_id 
FROM 
    user 
JOIN 
    userdetails ON user.id = userdetails.user_id 
JOIN 
    following on user.id = following.follow_id 
WHERE 
    following.follow_id = 2 

这就是我的桌子的样子

Following
id | user_id | follow_id

Tweets
user_id|id|date|message

user
id|email|password|username

userdetails
id|firstname|lastname|profile_img|user_id|about

有小费吗 ?

4

3 回答 3

2

更改JOIN following on user.id = following.follow_idJOIN following on user.id = following.user_id

SELECT 
    user.id, user.username, user.email,
    userdetails.profile_img, following.follow_id 
FROM 
    user 
JOIN 
    userdetails ON user.id = userdetails.user_id 
JOIN 
    following on user.id = following.user_id 
WHERE 
    following.follow_id = 2
于 2013-04-20T20:16:48.190 回答
1

试试这个

SELECT * FROM Following 
INNER JOIN Users on Following.UserId=Users.Id
INNER JOIN UsersDetails on Users.Id=UserDetails.UserId
where Following.Follow_id=@UserId

当然,您可以根据需要选择列。

于 2013-04-20T20:15:41.627 回答
1
SELECT user.id, user.username,user.email,
userdetails.profile_img, following.follow_id FROM user JOIN userdetails 
ON user.id = userdetails.user_id JOIN following 
ON user.id = following.follow_id WHERE following.follow_id = 2

要获取与follow_id= 2 匹配的所有用户 id:

SELECT user_id from following where follow_id = 2;

(我会将它们user_id在下表中的列名称更改为follower_id

还要从与关联的 userdetails 表中获取数据user_id

SELECT userdetails.*, user.* FROM userdetails JOIN users 
ON user.id = userdetails.user_id JOIN following
ON user.id = following.***user_id*** WHERE following.follow_id = 2;

看看这是否有效,我认为您的问题是您加入了关注。follow_id给用户而不是following.user_id

于 2013-04-20T20:16:35.707 回答