左连接与表一起使用但在查询时失败

时间:2012-03-28 16:32:46

标签: sql ms-access left-join

MS Access 2007中的以下左连接查询

SELECT
Table1.Field_A,
Table1.Field_B,
qry_Table2_Combined.Field_A,
qry_Table2_Combined.Field_B,
qry_Table2_Combined.Combined_Field
FROM Table1
LEFT JOIN qry_Table2_Combined
 ON (Table1.Field_A = qry_Table2_Combined.Field_A) 
AND (Table1.Field_B = qry_Table2_Combined.Field_B);
我希望

返回此结果:

+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|1       |         |         |         |                |
+--------+---------+---------+---------+----------------+
|1       |         |         |         |                |
+--------+---------+---------+---------+----------------+
|2       |1        |2        |1        |John, Doe       |
+--------+---------+---------+---------+----------------+
|2       |2        |         |         |                |
+--------+---------+---------+---------+----------------+

[Table1]有4条记录,[qry_Table2_Combined]有1条记录。

但它给了我这个:

+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|2       |1        |2        |1        |John, Doe       |
+--------+---------+---------+---------+----------------+
|2       |2        |2        |         |,               |
+--------+---------+---------+---------+----------------+

真的很奇怪[Combined_Field]在第二行有一个逗号。我使用逗号连接[qry_Table2_Combined]中的两个字段。

如果左连接查询使用从查询[qry_Table2_Combined]创建的表,它将按预期工作。

为什么这会使连接查询不能为查询和表提供相同的结果?如何使用左连接中的查询获得正确的结果?

3 个答案:

答案 0 :(得分:3)

看着你的逻辑,似乎你只想要field_A =“2”的组合字段(SELECT'2'AS Field_A)。我怀疑这是导致问题的原因。是否有可能以不同的方式解决问题,例如:

SELECT 
   t1.Field_A, 
   t1.Field_B, 
   t2.Field_B As t2B, 
   [t2].[Col_1] & ", " & [t2].[Col_2] AS Combined
FROM t1 LEFT JOIN t2 
ON t1.Field_B = t2.Field_B
WHERE t1.Field_A="2"
UNION ALL
SELECT  
   t1.Field_A, 
   t1.Field_B, 
   "None" As t2B, 
   "None" AS Combined
FROM t1 
WHERE t1.Field_A<>"2"

答案 1 :(得分:1)

连接:将&运算符更改为+运算符,结果应该符合预期。


缺少行:我可以重现这个问题,但不能解释它,除了说a)它可能是一个bug而且b)它可能永远不会得到修复:(

为了理智,我在SQL Server中测试了相同的代码,它按预期工作。

作为一般要点an outer join can be simulated using union and padding the missing values,例如伪代码:

( A JOIN B )
UNION
( A NOT MATCH B { A.*, <pad values for B> } )

在您的情况下和Access SQL中:

SELECT Table1.Field_A, Table1.Field_B,  
       qry_Table2_Combined.Field_A,  
       qry_Table2_Combined.Field_B,  
       qry_Table2_Combined.Combined_Field  
  FROM Table1  
       INNER JOIN qry_Table2_Combined  
          ON (Table1.Field_A = qry_Table2_Combined.Field_A)  
             AND (Table1.Field_B = qry_Table2_Combined.Field_B)
UNION ALL
SELECT Table1.Field_A, Table1.Field_B,  
       NULL AS Field_A,  
       NULL AS Field_B,  
       NULL AS Combined_Field  
  FROM Table1  
 WHERE NOT EXISTS ( SELECT * 
                      FROM qry_Table2_Combined 
                     WHERE (Table1.Field_A = qry_Table2_Combined.Field_A)  
                            AND (Table1.Field_B = qry_Table2_Combined.Field_B) );

以上似乎产生了你期待的结果。


使用连接修复访问repro代码,取消注释建议的解决方法代码:

Sub EXfewfTempler()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")

  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"

    With .ActiveConnection

      Dim Sql As String

      Sql = "CREATE TABLE Table1 (  Field_A VARCHAR(10),  Field_B VARCHAR(10) );"
      .Execute Sql

      Sql = "CREATE TABLE Table2 ( Field_B VARCHAR(10),  Col_1 VARCHAR(10),  Col_2 VARCHAR(10));"
      .Execute Sql

      Sql = "CREATE VIEW qry_Table2_Combined AS SELECT '2' AS Field_A, Table2.Field_B, Table2.Col_1 + ', ' + Table2.Col_2 AS Combined_Field FROM Table2; "
      .Execute Sql

      Sql = "INSERT INTO Table1 VALUES (1, NULL);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (1, NULL);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (2, 1);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (2, 2);"
      .Execute Sql
      Sql = "INSERT INTO Table2 VALUES (1, 'John', 'Doe');"
      .Execute Sql

      Sql = _
          "SELECT " & _
          "Table1.Field_A, " & _
          "Table1.Field_B, " & _
          "qry_Table2_Combined.Field_A, " & _
          "qry_Table2_Combined.Field_B, " & _
          "qry_Table2_Combined.Combined_Field " & _
          "FROM Table1 " & _
          "LEFT JOIN qry_Table2_Combined " & _
          " ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
          "AND (Table1.Field_B = qry_Table2_Combined.Field_B);"

'      Sql = _
'          "SELECT Table1.Field_A, Table1.Field_B, " & _
'          "       qry_Table2_Combined.Field_A, " & _
'          "       qry_Table2_Combined.Field_B, " & _
'          "       qry_Table2_Combined.Combined_Field " & _
'          "  FROM Table1 " & _
'          "       INNER JOIN qry_Table2_Combined " & _
'          "          ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
'          "             AND (Table1.Field_B = qry_Table2_Combined.Field_B) " & _
'          "UNION ALL " & _
'          "SELECT Table1.Field_A, Table1.Field_B, " & _
'          "       NULL AS Field_A, " & _
'          "       NULL AS Field_B, " & _
'          "       NULL AS Combined_Field " & _
'          "  FROM Table1 " & _
'          " WHERE NOT EXISTS ( SELECT * " & _
'          "                      FROM qry_Table2_Combined " & _
'          "                     WHERE (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
'          "                            AND (Table1.Field_B = qry_Table2_Combined.Field_B) );"


      Dim rs
      Set rs = .Execute(Sql)
      MsgBox rs.GetString(2, , vbTab & vbTab, , "<NULL>")

    End With
    Set .ActiveConnection = Nothing
  End With
End Sub

答案 2 :(得分:0)

这不是MSAccess解析的问题。对于测试,将查询中的字段名称更改为Field_C和Field_D,并查看是否仍有相同的问题