SQL在多个表之间连接

时间:2016-12-14 02:07:45

标签: sql join

我现在正撞在墙上。我已经用了很多时间而且已经过了一半,但放弃了我的查询的最后一部分。

我有三张桌子。

  1. R8表(此表包含有关计算机的信息)
  2. Actor表(这是客户,供应商和个人表)
  3. TXT table(此表包含R8和Actor表中某些字段的文本值)
  4. 我想要的结果是:

    R8.gr,R8.gr1,r8.gr2与TXT相关联。例如r8.gr2 = txt.txtno,我可以从中提取grx的文本值。这个问题我已经解决了。

    但是在同样的结果中我想要r8.custno.grx.txt意味着我首先必须在Actor表中查找custno,然后找到grx字段并查找此grX字段的txt。 TXT表。

    这是我的SQL查询,它给出了第一部分:

    SELECT      
        maskin.RNo, maskin.Nm, maskin.Gr5, tekst.Txt AS 'Produksjonsår',
        maskin.Gr7, tekst2.Txt AS 'Strøm', maskin.Gr8, 
        tekst3.Txt AS 'Eierforhold', maskin.Gr3, tekst4.Txt AS 'Båndtype'
    FROM            
        R8 AS maskin 
    LEFT OUTER JOIN
        Txt AS tekst3 ON maskin.Gr8 = tekst3.TxtNo AND tekst3.TxtTp = 93 
    LEFT OUTER JOIN
        Txt AS tekst4 ON maskin.Gr3 = tekst4.TxtNo AND tekst4.TxtTp = 38 
    LEFT OUTER JOIN
        Txt AS tekst2 ON maskin.Gr7 = tekst2.TxtNo AND tekst2.TxtTp = 20 
    LEFT OUTER JOIN
        Txt AS tekst ON maskin.Gr5 = tekst.TxtNo AND tekst.TxtTp = 40 
    

    我需要的建议/帮助是获取custno的txt(来自r8) - > gr(来自演员) - > txt(来自txt)

1 个答案:

答案 0 :(得分:0)

我认为这就是你要求的。我在您的查询和您在txt中寻找的字段中添加了第二个和第三个连接。

SELECT      
    maskin.RNo, maskin.Nm, maskin.Gr5, tekst.Txt AS 'Produksjonsår',
    maskin.Gr7, tekst2.Txt AS 'Strøm', maskin.Gr8, 
    tekst3.Txt AS 'Eierforhold', maskin.Gr3, tekst4.Txt AS 'Båndtype',
    myTxt.Txt
FROM            
    R8 AS maskin 
LEFT JOIN 
    Actor on maskin.CustNo = Actor.CustNo
LEFT JOIN 
    Txt AS myTxt On txt.myTxt = Actor.gr
LEFT OUTER JOIN
    Txt AS tekst3 ON maskin.Gr8 = tekst3.TxtNo AND tekst3.TxtTp = 93 
LEFT OUTER JOIN
    Txt AS tekst4 ON maskin.Gr3 = tekst4.TxtNo AND tekst4.TxtTp = 38 
LEFT OUTER JOIN
    Txt AS tekst2 ON maskin.Gr7 = tekst2.TxtNo AND tekst2.TxtTp = 20 
LEFT OUTER JOIN
    Txt AS tekst ON maskin.Gr5 = tekst.TxtNo AND tekst.TxtTp = 40