我正在尝试编写一个脚本,它将从表中给出不同的行,表A中有NULL值,但对于相同的列,表B有值。
场景:我有2个表: 表A和表B具有相同的列
表A中的值是
| ID | Name | LicenseNo | NationalLicenseNumber |
| 1 | 'ABC,XYZ' | NULL | NULL |
| 1 | 'CDS,ABD' | NULL | NULL |
表B的值如下:
| 1 | 'XYZ ABC' | 1234567 | 976633 |
| 1 | 'ABD CDS' | 4324223 | 304242 |
依旧...我想显示结果,显示两个表中所有列的值。 任何帮助,将不胜感激 感谢
答案 0 :(得分:1)
尝试此选择:
select a.ID,a.Name,a.LicenseNo,a.NationalLicenseNumber,
b.ID,b.Name,b.LicenseNo,b.NationalLicenseNumber
from TableA a
left outer join TableB b on a.ID=b.ID
where
((a.Name is null) and (b.Name is not null))
or ((a.Name is not null) and (b.Name is null))
((a.LicenseNo is null) and (b.LicenseNo is not null))
or ((a.LicenseNo is not null) and (b.LicenseNo is null))
((a.NationalLicenseNumber is null) and (b.NationalLicenseNumber is not null))
or ((a.NationalLicenseNumber is not null) and (b.NationalLicenseNumber is null))
它连接ID
上的两个表,并且只保留相应列的null-ness不同的行。
编辑替换了导致语法错误的(a.Name is null) <> (b.Name is null)
更长的表达式,而不是比较布尔值。
答案 1 :(得分:0)
尝试检查是否为空:
SELECT *
FROM TABLE_A
WHERE Name IS NOT NULL AND LicenseNo IS NOT NULL AND NationalLicenseNumber IS NOT NULL
UNION
SELECT *
FROM TABLE_B
WHERE Name IS NOT NULL AND LicenseNo IS NOT NULL AND NationalLicenseNumber IS NOT NULL
答案 2 :(得分:0)
这会奏效。此外,它还通过对TableB的columnNames进行别名来解决第二组列的问题,这些列具有相同的nam es,简单地返回为“Expr_1”,“Expr_2”等。 这将返回TableB中的所有行(根据您的帖子,其中包含所有正确的值)并将它们在ID字段中连接到TableA中的行(您可以在任何其他字段中使用NULL值的行)。这将返回ID匹配的两个表的记录,以及TableA中任何一个或多个相关字段为空的记录。
SELECT
a.ID,
a.Name,
a.LicenseNo,
a.NationalLicenseNo,
b.ID AS b_ID,
b.Name AS b_Name,
b.LicenseNo AS b_LicenseNo,
b.NationalLicenseNo AS b_NationalLicenseNo
FROM
TableA AS a RIGHT OUTER JOIN
TableB AS b ON a.A = b.A
WHERE
(a.Name IS NULL) OR
(a.LicenseNo IS NULL) OR
(a.NationalLicenseNo IS NULL)