2014-11-04 83 views
-1

好了,所以我有三个表与这些领域(当然也有更多的非相关):内连接没有直接关系

full_table
属性
条款ArticleID

MAIN_TABLE
商品ID

other_table
属性

所以在full_table中有“articleID”及其相关“属性”的完整列表。在main_table中只有一些articleID。在other_table中只有一些“属性”。

我想要的只是那些属性在other_table中且其articleID在main_table中的文章。

编辑:说明

problem description

问题的一些视觉上的帮助,是我的查询也返回他们中的很多不在other_table。

查询如下:

SELECT full_table.attribute, main_table.articleID FROM main_table 
INNER JOIN full_table ON full_table.articleID = main_table.articleID 
INNER JOIN other_table ON other_table.attribute = full_table.attribute 

EDIT2:我已经测试样品PIC和代码上的SQL小提琴,并根据需要工作,所以这个问题必须来自真正的查询增加了复杂性,我我们将继续研究问题的根源。

+2

请编辑您的问题,并提供样本数据和预期的结果。您的查询似乎会按照您的要求进行:“仅获取属性位于other_table中的那些文章”。 – 2014-11-04 13:21:34

+0

谢谢戈登,在这里你拿着样品和想要的结果;) – Pinx0 2014-11-04 14:01:56

+0

。 。你应该删除这个问题,因为这是由误解造成的。 – 2014-11-04 16:00:56

回答

0

所以问题是,它返回相同的行,每行3次,从而迷惑我,因为行的数量比预期的要大得多。但该查询正在过滤适当。

通过向所有选择的项目添加GROUP BY来解决此问题。

的解决办法是:

SELECT full_table.attribute, main_table.articleID FROM main_table 
INNER JOIN full_table ON full_table.articleID = main_table.articleID 
INNER JOIN other_table ON other_table.attribute = full_table.attribute 
GROUP BY full_table.attribute, main_table.articleID 
0

请提供外键关系,因此示例更清晰。然而,一目了然,你能做些什么:

SELECT ot.attribute, 
     ft.articleId 
FROM other_table ot 
INNER JOIN full_table ft ON ot.attribute = ft.attribute; 
0

从我理解。

DECLARE @full_table TABLE (articleID INT, attribute NVARCHAR(100)) 
DECLARE @main_table TABLE (articleID INT) 
DECLARE @other_table TABLE (attribute NVARCHAR(100)) 

INSERT INTO @full_table VALUES (1,'attrib1'),(2,'attrib2'),(3,'attrib3'),(4,'attrib4'),(5,'attrib5') 
INSERT INTO @main_table VALUES (2),(3),(5) 
INSERT INTO @other_table VALUES ('attrib1'),('attrib2'),('attrib5') 

SELECT a.* FROM @full_table as a 
INNER JOIN @other_table as b ON a.attribute = b.attribute