我有两个表格:
CREATE TABLE [dbo].[Friendships](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Sender] [nvarchar](50) NOT NULL,
[Recipient] [nvarchar](50) NOT NULL,
[IsPending] [int] NOT NULL,
[DateTime] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_Connections] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Friendships] ADD CONSTRAINT [DF_tbl_Connections_uc_IsPending] DEFAULT ((1)) FOR [IsPending]
GO
目 录
CREATE TABLE [dbo].[LiveStreams](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Value] [nvarchar](max) NOT NULL,
[Sender] [nvarchar](50) NOT NULL,
[Recipient] [nvarchar](50) NOT NULL,
[DateTime] [datetime] NOT NULL,
CONSTRAINT [PK_LiveStreams] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
我用下文(布赖恩建议):
"SELECT Id, CASE " +
"WHEN Sender = @UserName THEN Recipient " +
"ELSE Sender " +
"END AS Friend, IsPending, DateTime " +
"FROM Friendships " +
"WHERE (Sender = @UserName " +
"OR Recipient = @UserName) " +
"目 录 IsPending = @IsPending;";
However, I would like to return the rows from LiveStreams only if the sql statement matches. Meaning I want to have a method that will return all LiveStreams for all friends of @SomeUser. Getting the friends is not the problem (see query above), but getting the LiveStreams of all friends of @SomeUser if either the friend in LiveStream is under the Sender or Recipient column.
任何想法? 感谢。