SQL好友(连接)JOIN声明

时间:2011-10-31 18:51:50

标签: c# asp.net sql sql-server select

tbl_Connections:

uc_Id   uc_User uc_Connection   uc_IsPending    uc_DateTime
72  jasonramia  jeffreyramia    0   2011-10-29 16:42:49.000
74  jasonramia  beta    0   2011-10-29 16:55:38.000
75  jeffreyramia    beta    0   2011-10-29 20:36:11.000

tbl_LiveStream:

ls_Id   ls_Story    ls_User ls_Connection   ls_DateTime
30  test    jeffreyramia    jasonramia  2011-10-29 19:16:48.000
31  aheln   jasonramia  jasonramia  2011-10-29 19:17:48.000
32  test    jasonramia  beta    2011-10-29 19:27:02.000

只有当ls_User或ls_Connection是朋友从表tbl_Connections(uc_User或uc_Connection)连接到jeffreyramia时,我才需要从LiveStream返回*为jeffreyramia(或任何其他用户)

此处有更多详情:SQL SELECT From two tables (friendship) statement

提前谢谢你。 :)

1 个答案:

答案 0 :(得分:0)

使用Union。这应该接近了。

SELECT * 
FROM tbl_Connections AS connect 
INNER JOIN
tbl_LiveStream AS live
WHERE connect.us_User = live.ls_User
UNION
SELECT * 
FROM tbl_Connections AS connect 
INNER JOIN
tbl_LiveStream AS live
WHERE connect.us_User = live.ls_Connection

如果C#可用于封装这些表的类,您也可以使用LINQ to SQL,但是这里没有显示很多细节。