0

我需要有关 SQL 查询的帮助。

我有两张桌子。一个是用户,另一个是用户朋友

users table:
aid email                    firstname 
1   example@mail.com         example
2   example2@mail.com        example2
3   example3@mail.com        example3
4   example4@mail.com        example4

userfriends tables:
reqid     email                    friendemail               status 
1         example1@mail.com        example2@mail.com            1       (example1 is frnds with example2)
2         example2@mail.com        example4@mail.com            2       (example2 request pending)
3         example3@mail.com        example1@mail.com            1       (example1 is frnds with example3)
4         example4@mail.com        example1@mail.com            1       (example1 is frnds with example4)

因此,当状态为 2 时,添加请求处于待处理状态,而状态为 1 时,他们是朋友。我想要的是我想检索用户 example1 的完整好友列表。我想从用户表中提取名称以获取先前查询的相应输出以显示为好友列表。

4

3 回答 3

1
select distinct friendemail
from userfriends f
inner join users u on u.email = f.email
where f.status = 1
and u.firstname = 'example'
于 2012-10-23T23:00:01.247 回答
1

我认为你们错过了搜索电子邮件可能在任一列中的事实:

select u.firstname
from userfriends f, users u where 
u.email='email@domain.com' and f.status=1 and
(u.email = f.email and f.friendsemail='email@domain.com')
or
(u.email = f.friendsemail and f.email='email@domain.com')
于 2012-10-23T23:02:17.123 回答
0

您需要JOIN两个表:

SELECT users.firstname 
    FROM userfriends UF 
    JOIN users U ON (U.email=UF.friendemail)
WHERE UF.email='your user'
于 2012-10-23T22:59:41.420 回答