0

我创建了 3 个表:

  • Login(firstname,lastname,age,gender,email)
  • Scraps(email,scrap,posttime)
  • requests(email,frnemail,frnshpstatus,frnpic)

我想要一个连接这三个表的查询,以显示用户和 frnshpstatus 为“Y”的人发布的关于他在请求表中的电子邮件的剪贴簿留言。

4

2 回答 2

0

您可以尝试连接查询。

DECLARE @usrEmail varchar(255) // Set the users email you are getting.

SELECT l.*, s.scrap, s.posttime
FROM Login l
LEFT JOIN Scraps s
ON l.email = s.email
WHERE l.email = @usrEmail

// Use a UNION query to also get scraps left by the users friend.
// Im assuming the friends would have entries in the login column as well.
UNION ALL

SELECT l.*, s.scrap, s.posttime
FROM Login l
LEFT JOIN Scraps s
ON l.email = s.email
WHERE l.email IN (SELECT frnshpEmail FROM requests WHERE email = @usrEmail AND frnshpStatus = 'Y') // This will create a list of all the users that are also have a frnshp with the user.

我不确定您如何使用请求表。如果您可以给我更多详细信息,我会尽力帮助您了解具体情况,但是就我所拥有的而言,这应该让您知道该怎么做。

于 2012-08-25T04:25:33.670 回答
0

试试这个:

select * from scraps where email in(
select r.frnemail
from [Login] l inner join requests r
on l.email=r.email and r.frnshpstatus = 'Y'
where l.email=@useremail)
于 2012-08-25T04:55:32.767 回答