可以搞清楚这个查询

时间:2013-04-26 11:46:56

标签: sql-server left-join

好的,我有以下表格

+-------------------+
|      FMdonnee     |
+-------------------+
| uGuid             |
| ...               |
| uGuid_FMchampForm |
| uGuid_FMdoc       |
+-------------------+

+-------------------+
|   FMchampForm     |
+-------------------+
| uGuid             |
| ...               |
| uGuid_FMgroup     |
| uGuid_FMsection   |
+-------------------+

+-------------------+
|      FMgroup      |
+-------------------+
| uGuid             |
| ...               |
+-------------------+

uGuid是所有表的主键,它的类型是uniqueidentifier

FMchampForm总是有数据,但它的外键uGuid_FMgroup可能为空。 FMdonnee表中可能有一些数据。我正在尝试从FMchampForm获取与相应的FM部分(FMchampform的fk)相匹配的所有数据,并且如果在FMchampForm的此条目中附加了FMdonnee或FMgroup中的数据,我想要接收它们。

我尝试了以下内容。女巫差不多好,但如果FMdonnee没有附加数据,它将不会接受FMchampForm条目

SELECT  cf.cNom                     nom_cf
          , cf.iPosition                pos_cf
          , cf.cTypeValeur              typVal_cf
          , cf.bActif                   actif_cf
          , cf.cCommentaire             com_cf
          , cf.bLabelVisible            lblVisible_cf
          , cf.cPadding                 pad_cf
          , cf.bMultiLines              multiLines_cf
          , cf.bStartLine               startLine_cf
          , g.cNom                      nom_g
          , g.iPosition                 pos_g
          , g.iNbRepTemplate            nbRepTemplate_g
          , g.cCommentaire              com_g
          , g.cPadding                  pad_g
          , g.bLabelVisible             lblVisible_g
          , g.bIsBreakable              isBreakable_g
          , d.cValeur                   valeur_d
          , d.cGroupe                   groupe_d
          , d.uGuid_FMunite             guidUnite_d
FROM FMdonnee d, FMchampForm cf
LEFT JOIN FMgroup g ON cf.uGuid_FMgroup = g.uGuid
WHERE cf.uGuid_FMsection = @guidFMsection
AND d.uGuid_FMchampForm = cf.uGuid
AND d.uGuid_FMdoc = @guidFMdoc
ORDER BY g.iPosition ASC, d.cGroupe ASC, cf.iPosition ASC, cf.cNom ASC

我想我需要LEFT JOIN FMdonnee,但我不知道如何在FMchampForm上加入它。我希望我很清楚应该让你理解我正在做的事情

1 个答案:

答案 0 :(得分:1)

由于FMdonnee记录可能不存在,您需要从FMchampForm LEFT JOIN到FMdonnee。尝试:

SELECT  cf.cNom                     nom_cf
          , cf.iPosition                pos_cf
          , cf.cTypeValeur              typVal_cf
          , cf.bActif                   actif_cf
          , cf.cCommentaire             com_cf
          , cf.bLabelVisible            lblVisible_cf
          , cf.cPadding                 pad_cf
          , cf.bMultiLines              multiLines_cf
          , cf.bStartLine               startLine_cf
          , g.cNom                      nom_g
          , g.iPosition                 pos_g
          , g.iNbRepTemplate            nbRepTemplate_g
          , g.cCommentaire              com_g
          , g.cPadding                  pad_g
          , g.bLabelVisible             lblVisible_g
          , g.bIsBreakable              isBreakable_g
          , d.cValeur                   valeur_d
          , d.cGroupe                   groupe_d
          , d.uGuid_FMunite             guidUnite_d
FROM FMchampForm cf
LEFT JOIN FMdonnee d 
       ON d.uGuid_FMchampForm = cf.uGuid AND d.uGuid_FMdoc = @guidFMdoc
LEFT JOIN FMgroup g ON cf.uGuid_FMgroup = g.uGuid
WHERE cf.uGuid_FMsection = @guidFMsection
ORDER BY g.iPosition ASC, d.cGroupe ASC, cf.iPosition ASC, cf.cNom ASC

(作为旁注,在同一个查询中混合隐式和显式连接语法是个坏主意,因为它可能特别令人困惑。)