加入用户和关注者表

时间:2014-06-30 08:40:16

标签: sql sql-server tsql select join

我有用户表和关注表 在下表中,我有用户ID - 和 - 关注者ID

我必须列出我必须使用用户和关注者的电子邮件ID的列表

我如何加入两个表

select f.USER_ID ,a.user_firstname, a.user_email ,f.Follow_Id 
from bb_follow f, bb_user a 
where a.user_id = f.User_Id and f.Active_Status=1 order by f.User_Id asc

(现在我想要关注者的信息 - 我可以添加的方式) -

3 个答案:

答案 0 :(得分:1)

加入 bb_user 表两次,别名为 a (针对用户)和 b (针对关注者)。

select f.USER_ID, a.user_firstname, a.user_email,
      f.FOLLOW_ID, b.user_firstname as follower_firstname, b.user_email as follower_email
from  bb_follow f, bb_user a, bb_user b 
where a.user_id = f.User_Id
and   b.user_id = f.Follow_Id
and   f.Active_Status=1

或使用ANSI连接:

select f.USER_ID, a.user_firstname, a.user_email,
      f.FOLLOW_ID, b.user_firstname as follower_firstname, b.user_email as follower_email
from  bb_follow f
      join bb_user a on a.user_id = f.User_Id
      join bb_user b on b.user_id = f.Follow_Id
where f.Active_Status=1

答案 1 :(得分:0)

试试这个:

SELECT f.USER_ID, a.user_firstname userName, a.user_email userEmail, 
       f.Follow_Id, b.user_firstname followerName, b.user_email followerEmail
FROM bb_follow f
LEFT JOIN bb_user a ON a.user_id = f.User_Id 
LEFT JOIN bb_user b ON b.user_id = f.Follow_Id 
WHERE f.Active_Status=1 
ORDER BY f.User_Id ASC;

答案 2 :(得分:-1)

对你来说总体而言,语法错误,你需要加入

SELECT * FROM bb_follow f, 加入bb_user a ON f.User_id = a.user_id 在哪里f.Active_Status =' 1'

尝试,然后添加您的特定值