使用AND链接从中间表中过滤数据

时间:2010-08-28 11:30:12

标签: sql-server-2005 linq-to-entities model-associations

我有以下情况:

表和列(数据库类型:Microsoft Sql Server 2005):

表:条目

  • 的EntryID
  • ... 其他不重要的列

表:属性

  • 属性Id

表:EntryAttributes

  • EntryID [Releation To:Entries-> EntryID]
  • AttributeID [Releation To:Attributes-> AttributeID]

那么,我如何只选择条目如何包含多个属性,如下面的SQL语句(该语句不适用于 AND 链接):

SELECT *
FROM  [Entries] AS [t0]
INNER JOIN [EntryAttributes] AS [t1] ON [t0].[EntryID] = [t1].[EntryID]
WHERE ([t1].[AttributeID] = 1) AND ([t1].[AttributeID] = 1)

如何构建正确的SQL语句?我敢肯定,某处已有解决方案,但我不知道应该使用哪些关键字来搜索该问题。

我使用'Linq to Entites',所以也许有一个简单的Linq表达式。

感谢您阅读我的帖子!

祝你好运!

1 个答案:

答案 0 :(得分:0)

此场景的解决方案是为我选择的每个[AttributeID]加入:

SELECT * 
FROM  [Entries] AS [t0] 
INNER JOIN [EntryAttributes] AS [t1] ON [t0].[EntryID] = [t1].[EntryID] 
INNER JOIN [EntryAttributes] AS [t2] ON [t0].[EntryID] = [t2].[EntryID] 
WHERE ([t1].[AttributeID] = 1) AND ([t2].[AttributeID] = 2)