SQL查询未返回正确的结果

时间:2016-05-17 09:17:23

标签: sql sql-server-2005

我有一个带有多个连接的查询,即使DOC_TYPE列中有一些值,它也会变为NULL。查询在

下面
SELECT 
    a.mkey,
    c.type_desc DOC_TYPE,
    a.doc_no INWARD_NO,
    CONVERT(VARCHAR, a.doc_date, 103) date,
    a.to_user,
    a.No_of_pages,
    Ref_No,
    c.type_desc DEPT_RECEIVED,
    c.type_desc EMP_RECEIVED,
    b.first_name + ' ' + b.last_name NAME,
    b.email
FROM
    inward_doc_tracking_hdr a
LEFT JOIN 
    user_mst b ON a.to_user = b.mkey
LEFT JOIN 
    type_mst_a c ON a.doc_type = c.master_mkey
                 AND a.dept_received = c.Master_mkey
                 AND a.emp_received = c.Master_mkey
WHERE  
    a.to_user = '1279' 

doc_type值为428,其解释来自

select type_desc 
from type_mst_a 
where master_mkey = 428

Drawing,但是当我运行连接查询时,我将其视为NULL。为什么??

我正在使用SQL Server 2005。

4 个答案:

答案 0 :(得分:1)

讨论后的当前版本是

SELECT
  a.mkey, c.type_desc DOC_TYPE, a.doc_no INWARD_NO, 
  convert(varchar, a.doc_date,103) date, a.to_user, a.No_of_pages, Ref_No, d.type_desc DEPT_RECEIVED, 
  b.first_name + ' ' + b.last_name SENDER, b.first_name + ' ' + b.last_name   NAME, b.email 
FROM inward_doc_tracking_hdr a 
-- LEFT ?
JOIN user_mst b ON a.to_user = b.mkey 
JOIN type_mst_a c ON a.doc_type = c.master_mkey 
JOIN type_mst_a d ON a.dept_received = d.Master_mkey 
WHERE 
  a.to_user = '1279'

如果结果中必须存在带有NULL或没有匹配项的inward_doc_tracking_hdr行,则需要LEFT JOIN。 希望我们现在走在正确的轨道上。

答案 1 :(得分:0)

因为您正在执行左连接,所以需要满足join子句中的所有条件以从连接表返回值,否则它们将为null。即。

   a.doc_type = c.master_mkey AND
   a.dept_received = c.Master_mkey AND
   a.emp_received = c.Master_mkey

如果你将LEFT JOIN改为INNER JOIN,我打赌你没有得到任何结果吗?

如果您只是在两个表上执行select,查看master_mkey = 428,您应该看到其中一个字段(a.dept_received,a.emp_received)的值不同于428,这就是您获得的原因一个空。

答案 2 :(得分:0)

我认为将LEFT OUTER JOIN更改为INNER JOIN可以解决您的问题。 (因为你从Left表中获取doc_no)

   SELECT a.mkey,
           c.type_desc                        DOC_TYPE,
           a.doc_no                           INWARD_NO,
           CONVERT(VARCHAR, a.doc_date, 103)  date,
           a.to_user,
           a.No_of_pages,
           Ref_No,
           c.type_desc                         DEPT_RECEIVED,
           c.type_desc                         EMP_RECEIVED,
           b.first_name + ' ' + b.last_name    NAME,
           b.email
    FROM   inward_doc_tracking_hdr a
    INNER JOIN user_mst b
      ON (a.to_user = b.mkey)
    INNER JOIN type_mst_a c
      ON (a.doc_type = c.master_mkey
         AND a.dept_received = c.Master_mkey
         AND a.emp_received  = c.Master_mkey)
    WHERE  a.to_user = '1279' 

答案 3 :(得分:0)

您必须使用内部联接而不是左连接才能获取具有doc_type的记录。此查询可以帮助您:

SELECT a.mkey,
       c.type_desc                      DOC_TYPE,
       a.doc_no                         INWARD_NO,
       CONVERT(VARCHAR, a.doc_date, 103)date,
       a.to_user,
       a.No_of_pages,
       Ref_No,
       c.type_desc                      DEPT_RECEIVED,
       c.type_desc                      EMP_RECEIVED,
       b.first_name + ' ' + b.last_name NAME,
       b.email
FROM   inward_doc_tracking_hdr a
       INNER JOIN user_mst b
              ON a.to_user = b.mkey
       INNER JOIN type_mst_a c
              ON a.doc_type = c.master_mkey
                 AND a.dept_received = c.Master_mkey
                 AND a.emp_received = c.Master_mkey
WHERE  a.to_user = '1279'