MYSQL FULL OUTER JOIN - 使用LEFT-UNION-LEFT JOIN时所有NULL结果

时间:2017-09-19 17:22:52

标签: mysql left-join on-clause

Tbl_A

select 
sd.[student_number],
a.[health_alert],
a.[comments],
b.[health_alert],
b.[comments]

from student_enrollmentcube as se,
   student_demographicscube as sd

cross apply (select  a.[health_alert], a.[comments], (select row_number() 
 over (partition by a.[student_id] order by a.[student_id]) as r1 from 
   ar_student_health_alerts) a)  where a.[student_id] = sd.[student_id] and 
    r1 = 1)
cross apply (select  b.[health_alert], b.[comments], (select row_number() 
 over (partition by b.[student_id] order by b.[student_id]) as r2 from 
  ar_student_health_alerts) b)  where b.[student_id] = sd.[student_id] and 
   r2 = 2)

where se.student_id = sd.student_id and
     se.enrollment_status= 'active' and
     se.[current_academic_year] = 'y'

Tbl_B

cap_id| yr_a| sp_a| iso_a| area_a| qty_a | prod_a |
     3| 2015|  TRR|    54|      8|   120 |      0 |
   678| 2015|  BOM|    62|     27|   0.0 |      0 |
    20| 2015|  TRR|    54|     27|   0.0 |      0 |
    45| 2015|  FRC|     7|     15| 86800 |      0 |
    52| 2015|  AZB|    12|      6|   987 |      0 |

我得到了我正在寻找的全部加入

但是查询也生成了一大堆所有NULL记录(在Tbl_C中id为#7)

Tbl_C - 最终Tbl

aqua_id| yr_b| sp_b| iso_b| area_b| qty_b | prod_b |
     78| 2015|  OTS|    32|     27|  6868 |      1 |   
    333| 2015|  FRC|     7|     15|   550 |      1 |
    334| 2015|  FRC|     7|     15|   550 |      2 |      
    789| 2015|  TRR|    54|     27| 45000 |      3 |
    987| 2015|  TRR|    32|     27|    40 |      2 |

我试图解决导致额外多个所有NULL记录的原因吗?

使用的查询是:

id| cap_id| aqua_id| yr_a| yr_b| sp_a| sp_b| iso_a| iso_b|area_a|area_b| qty_a| qty_b | prod_a | prod_b
1 |     20|     789| 2015| 2015|  TRR|  TRR|    54|    54|    27|    27|   0.0| 45000 | 0      | 1
2 |     45|     333| 2015| 2015|  FRC|  FRC|     7|     7|    15|    15| 86800| 550   | 0      | 1
3 |     45|     334| 2015| 2015|  FRC|  FRC|     7|     7|    15|    15| 86800| 550   | 0      | 2
4 |    678|    NULL| 2015| NULL|  BOM| NULL|    62|  NULL|    27|  NULL|   0.0| NULL  | 0      | NULL
5 |      3|    NULL| 2015| NULL|  TRR| NULL|    54|  NULL|     8|  NULL|   120| NULL  | 0      | NULL
6 |   NULL|      78| NULL| 2015| NULL|  OTS|  NULL|    32|  NULL|    27|  NULL| 6868  | 0      | 1
7 |   NULL|     987| NULL| 2015| NULL|  TRR|  NULL|    32|  NULL|    27|  NULL| 40    | 0      | 2
8 |   NULL|    NULL| NULL| NULL| NULL| NULL|  NULL|  NULL|  NULL|  NULL|  NULL| NULL  | NULL   | NULL
9 |   NULL|    NULL| NULL| NULL| NULL| NULL|  NULL|  NULL|  NULL|  NULL|  NULL| NULL  | NULL   | NULL

3 个答案:

答案 0 :(得分:0)

DEMO:http://rextester.com/AWDYA21027使用您的示例数据&查询我没有得到重复的空列。这意味着数据问题。

为什么要将它作为左连接?只需从左向右切换,将where子句切换为b.yr_a。

并消除空值,通过消除所有4个值为null的记录,确保至少有一个连接条件匹配?

根据您的样本数据,将返回完整的外部联接:按cap_Id和aqua_ID排序

+--------+---------+------+------+-------+-------+
| CAP_ID | AQUA_ID | YR_A | YR_B | QTY_A | QTY_B |
+--------+---------+------+------+-------+-------+
|        |      78 |      | 2015 |       |  6868 |
|        |     987 |      | 2015 |       |    40 |
|      3 |         | 2015 |      |   120 |       |
|     20 |     789 | 2015 | 2015 |     0 | 45000 |
|     45 |     333 | 2015 | 2015 | 86800 |   550 |
|    678 |         | 2015 |      |     0 |       |
+--------+---------+------+------+-------+-------+

以下查询返回:

+----+--------+---------+------+------+----------+----------+
|    | cap_id | aqua_id | yr_a | yr_b |  qty_a   |  qty_b   |
+----+--------+---------+------+------+----------+----------+
|  1 | NULL   | 78      | NULL | 2015 | NULL     | 6868,00  |
|  2 | NULL   | 987     | NULL | 2015 | NULL     | 40,00    |
|  3 | 3      | NULL    | 2015 | NULL | 120,00   | NULL     |
|  4 | 20     | 789     | 2015 | 2015 | 0,00     | 45000,00 |
|  5 | 45     | 333     | 2015 | 2015 | 86800,00 | 550,00   |
|  6 | 678    | NULL    | 2015 | NULL | 0,00     | NULL     |
+----+--------+---------+------+------+----------+----------+

这似乎是完整外连接的正确结果。 如果我在A或B中有重复的值,则此查询返回那些重复项,因为UNION中的distinct不会发生,因为我们正在进行union all;不是工会。

(SELECT a.cap_id, b.aqua_id, a.yr_a, b.yr_b, a.qty_a, b.qty_b
FROM tbl_A AS a 
LEFT JOIN tbl_B AS b
  ON a.yr_a = b.yr_b 
 AND a.iso_a = b.iso_b
 AND a.area_a = b.area_b
 AND a.sp_a = b.sp_b
WHERE a.yr_a = 2015
  and (a.yr_a is not null 
   or a.iso_a is not null 
   or a.area_a is not null 
   or a.sp_a is not null))
UNION ALL
(SELECT a.cap_id, b.aqua_id, a.yr_a, b.yr_b, a.qty_a, b.qty_b
FROM tbl_A AS a 
RIGHT JOIN tbl_B AS b
   ON a.yr_a = b.yr_b 
  AND a.iso_a = b.iso_b
  AND a.area_a = b.area_b
  AND a.sp_a = b.sp_b
WHERE b.yr_b = 2015
  and (b.yr_b is not null 
   or b.iso_b is not null 
   or b.area_b is not null 
   or b.sp_b is not null)
 and a.iso_a is null #to exclude extra nulls duplicated by union all.
 );

答案 1 :(得分:0)

生成NULLS是因为: 没有" IS NULL"在第二个LEFT JOIN中的一个ON子句变量的WHERE子句中,INNER JOIN运行两次! 事实上我加入了一个较短的表来加入较长的表(row_#为Tbl_B< Tbl_A),因此在Tbl_b中没有记录所有Tbl_A 2015记录的事实。

答案 2 :(得分:0)

答案似乎很简单。 如果你选择第一个并运行它:

SELECT a.*, b.*
FROM Tbl_A AS a LEFT JOIN Tbl_B AS b
ON a.yr_a = b.yr_b 
AND a.iso_a = b.iso_b
AND a.area_a = b.area_b
AND a.sp_a = b.sp_b
WHERE a.yr_a = 2015

你应该得到5个结果行,因为5行是Tbl_A。问题在于,由于连接标准,并非所有这些线都与Tbl_B中的线连接。因此,当您连接这些表时,对于Tbl_A的某些行,将会有Tbl_B的列,这些列将为NULL,因为连接是外部的。

同样适用于您的第二个查询:

SELECT a.*, b.*
FROM  Tbl_B AS b LEFT JOIN Tbl_A AS a
ON a.yr_a = b.yr_b
AND a.iso_a = b.iso_b
AND a.area_a = b.area_b
AND a.sp_a = b.sp_b
WHERE b.yr_b = 2015

来自Tbl_B的行没有与Tbl_A的精确连接,因此你离开了连接到B,你将有Tbl_B的行有很多NULL。

我建议你逐个运行它们并查看每个查询的结果。