SQL查询获取基于parentid表B的记录表A.

时间:2012-04-17 21:36:28

标签: sql sql-server

[已解决:感谢bluefeet和Brian我必须构建这个工作查询(原来我还需要第四张表/视图)]“

SELECT        A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM            VIEWA AS A 
                INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID 
                AND B.Name = N'Collect' AND DAPR.Value = 1 
                INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID 
                AND C.Name='Assemble' 
                AND C.Value = 1 
                INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId 
                AND D.Name = 'IndexY'
WHERE        (A.SalesID = @SalesID)

[更新:假设IndexY / Color和ProdId我犯了一个错误,其中字段表A是某种参数/属性表,只有5列ID - NAME - VALUE - ARTICLERESULTID - PRODID。 IndexY是Name字段的值..]

我无法构建正确的sql查询来完成这个技巧:

我有以下2个表格:

Table A 
ID Name         Value   ArticleResultID     ProdID    Color
1  Operation    Collect             110         10    BLACK
2  IndexY            10             110         10    - 
3  Operation    Collect             101         11    WHITE


Table B
ID  ParentID    Name        Value       
101 110     Assemble        1       
101 100     Assemble        0     

步骤:

Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A

在上面的场景中,ORG_A.ArticleResultID = 11 - > B.ParentID = 110 - > NEW_A.ARTICLERESULTID = 110 - > (IndexY - Value - 10)

非常感谢,如果有人可以告诉我如何构建此查询..

致以最诚挚的问候,

Mike D

[OLD DESCRIPTION:]

我无法构建正确的sql查询来完成这个技巧:

我有以下2个表格:

Table A 
Name        Value   ArticleResultID      IndexY      Color  ProdID
Operation   Collect             110         10           -       0
Operation   Collect             101         _        Black     100  


Table B
ID  ParentID    Name        Value   Dx      Dy
101 110     Assemble        1       1000    500 
101 100     Assemble        0       400     300

我想从A中获取所有记录,其中NAME等于'operation',VALUE等于'Collect'和PRODID ='100'但我也希望(这是我的问题)表A中记录的IndexY值与PARENTID在表B中加入表B.ID = A.ArticleResultID AND Name ='Assemble'和VALUE ='1'

在上面的场景中,ParentID 110给了我表A中的记录,其中ArticleResultID 110带有IndexY(10)。

非常感谢,如果有人可以告诉我如何构建此查询..

致以最诚挚的问候,

Mike D

2 个答案:

答案 0 :(得分:2)

这对你有用吗?

SELECT a.*,c.IndexY
  FROM [TableA] a
  JOIN [TableB] b ON a.ArticleResultId = b.id
                 AND b.Name ='Assemble'
                 AND b.Value = 1
  JOIN [TableA] c ON b.ParentId = c.ArticleResultID
  WHERE a.name = 'Operation'
    AND a.Value = 'Collect'
    AND a.ProdId = 100

答案 1 :(得分:2)

因为您的要求不是最清楚的。怎么样:

SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
    ON a1.articleresultid = b.id
INNER JOIN ta a2
    ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
    AND a1.prodid = 100
    AND b.name = 'Assemble'
    AND b.value = 1

这是一个sqlfiddle,其工作版本

如果您只想要110 articlereulti的记录,那么:

SELECT a2.*
FROM ta a1
INNER JOIN tb b
    ON a1.articleresultid = b.id
INNER JOIN join ta a2
    ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
    AND a1.prodid = 100
    AND b.name = 'Assemble'
    AND b.value = 1

请参阅sqlfiddle获取第二个示例