SQL查询在我的JOIN练习中不起作用

时间:2018-11-14 14:46:44

标签: sql sql-server join left-join inner-join

我正在使用SQL查询,但查询结果为12行,需要为3行;我不知道。

查询无效:

SELECT g.*
    ,c.TxtFobFiyati
    ,C.TxtTalepETeslimTarihi
    ,c.TslParaBirimi
    ,c.TxtFiyat
    ,C.TslDepoKodu
    ,c.TslOlcuBirimi AS 'Olcu'
    ,j.TxtAmbalajDara
    ,j.TxtPaletDara
    ,g.TxtAdet AS 'ambalajAdet'
    ,g.TxtPaletSayisi AS 'paletSayisi'
    ,j.TxtAmbalajAdi
    ,J.TxtAmbalajNetKG
    ,j.TxtBoxGrossWeight
    ,C.TxtAciklama AS 'KalemAciklama'
    ,G.TxtAciklama AS 'KalemAciklama2'
    ,g.TxtPaletUstu AS 'PaletUstu'
    ,g.TxtPaletSayisi AS 'PaletSayisi'
    ,c.TxtUrunAdiEng AS 'UrunAdi_Ingilizce'
    ,C.TxtFobFiyati AS 'FobFiyati'
    ,j.TslPaletTipi AS 'PaletTipi'
    ,j.TxtPaletDara AS 'PaletDara'
FROM E_KT_SAS_Form A
LEFT JOIN E_KT_SAS_Form_DtyUrunler2 B
    ON A.ID = B.FORMID
LEFT JOIN E_KT_SAS_FrmSipUrunEkle C
    ON B.DOCUMENTID = C.ID
LEFT JOIN E_KT_SAS_Form_DtyTeyitEdilen2 D
    ON A.ID = D.FORMID
LEFT JOIN E_KT_SAS_FrmUretimTarihiEkle E
    ON D.DOCUMENTID = E.ID
        AND E.TslUrun = C.TslUrunKodu
LEFT JOIN E_KT_SAS_Form_DtySevkiyatlar F
    ON A.ID = F.FORMID
LEFT JOIN E_KT_SAS_FrmSevkiyatEkle G
    ON F.DOCUMENTID = G.ID
LEFT JOIN E_KT_SAS_FrmSipUrunEkle_DtyAmbalajlama H
    ON C.ID = H.FORMID
LEFT JOIN E_KT_SAS_FrmUrunAmbalajlama J
    ON H.DOCUMENTID = J.ID
WHERE E.TslOnay_TEXT = 'Evet'
    AND C.TslUrunKodu = G.TslUrun
    AND A.ID = 11682

无效结果

enter image description here

正确的结果表

enter image description here

1 个答案:

答案 0 :(得分:0)

第1步)从驱动程序表开始计数,即您别名为“ A”的表。检查计数是否为3.继续。 对每个新的连接重复步骤1,然后查看计数达到12的地方。那是您麻烦的连接。如果可能,将其注释掉,然后继续加入。如果无法连续进行联接,则找到最佳解决方案以将计数降低到3。

如果您确信where子句很棒;否则也没有它。如果计数大于3。则将其重新弹出。

  select count(1) 
    FROM E_KT_SAS_Form A
    LEFT JOIN E_KT_SAS_Form_DtyUrunler2 B ON A.ID = B.FORMID
    LEFT JOIN E_KT_SAS_FrmSipUrunEkle C  ON B.DOCUMENTID = C.ID
    LEFT JOIN E_KT_SAS_Form_DtyTeyitEdilen2 D ON A.ID = D.FORMID
    LEFT JOIN E_KT_SAS_FrmUretimTarihiEkle E ON D.DOCUMENTID = E.ID
            AND E.TslUrun = C.TslUrunKodu 
    LEFT JOIN E_KT_SAS_Form_DtySevkiyatlar F ON A.ID = F.FORMID
    LEFT JOIN E_KT_SAS_FrmSevkiyatEkle G ON F.DOCUMENTID = G.ID
    LEFT JOIN E_KT_SAS_FrmSipUrunEkle_DtyAmbalajlama H ON C.ID = H.FORMID
    LEFT JOIN E_KT_SAS_FrmUrunAmbalajlama J ON H.DOCUMENTID = J.ID
    WHERE E.TslOnay_TEXT = 'Evet' AND C.TslUrunKodu = G.TslUrun AND A.ID = 11682