美好的一天,
我已经阅读了人们在左连接方面遇到的一些其他问题,没有给出正确的结果,也许我不能理解所有答案。
这是我在查询中遇到的问题
SELECT
TA.column1
,D.column2
,D.column3
,TA.column4
,MI.[column5]
,MI.[column6]
,D.[column7]
,D.[column8]
,TA.[column9]
,TA.[column10]
,UC.[column11]
,UC.[column12]
,TA.[column13]
,TA.[column14]
,TA.[column15]
,TA.[column16]
,TA.[column17]
,'0' AS column18
,TA.[column19]
,'12.2' AS column20
,'None' AS column21
,'0' AS column22
,'1' AS column23
,TA.[column24]
,'0' AS column25
,'0' AS column26
,LD.column27
FROM [DB].[dbo].[Table1] AS TA WITH (NOLOCK)
FULL OUTER JOIN
[DB].[dbo].[Table2] AS LD WITH (NOLOCK)
ON
TA.[coulmn1]=LD.[coulmn5]
INNER JOIN
[DB].[dbo].[Table3] AS MI WITH (NOLOCK)
ON
TA.coulmn10 = MI.[coulmn3]
LEFT JOIN
[DB].[dbo].[Table4] AS D WITH (NOLOCK)
ON
TA.coulmn25 = D.coulmn1
INNER JOIN
[DB1].[dbo].[Table5] AS UC WITH (NOLOCK)
ON
TA.coulmn12 = UC.coulmn3
WHERE
(LD.coulmn5 != TA.[coulmn1])
AND
(TA.[coulmn23]= '0')
问题是我更新查询并删除
FULL OUTER JOIN
[DB].[dbo].[Table2] AS LD WITH (NOLOCK)
ON
TA.[coulmn1]=LD.[coulmn5]
以及删除以下where子句:
WHERE (LD.coulmn5 != TA.[coulmn1]) AND
我在右边的桌子上查看了3个结果。这是正确的,因为一个结果尚未写入左表
当我放回左外连接和where子句时我只会再次看到2个结果,如果最后一列,LD.column27
显示为NULL
,则不会有三个结果。
这一栏是接受NOT NULL
这可能是我没有得到首选结果的原因吗?
答案 0 :(得分:0)
使用LEFT OUTER JOIN
代替FULL OUTER JOIN
并删除`WHERE(LD.coulmn5!= TA.coulmn1))和'部分
试试这个:
SELECT
TA.column1
,D.column2
,D.column3
,TA.column4
,MI.[column5]
,MI.[column6]
,D.[column7]
,D.[column8]
,TA.[column9]
,TA.[column10]
,UC.[column11]
,UC.[column12]
,TA.[column13]
,TA.[column14]
,TA.[column15]
,TA.[column16]
,TA.[column17]
,'0' AS column18
,TA.[column19]
,'12.2' AS column20
,'None' AS column21
,'0' AS column22
,'1' AS column23
,TA.[column24]
,'0' AS column25
,'0' AS column26
,LD.column27
FROM [DB].[dbo].[Table1] AS TA WITH (NOLOCK)
LEFT OUTER JOIN [DB].[dbo].[Table2] AS LD WITH (NOLOCK) ON TA.[coulmn1]=LD.[coulmn5]
INNER JOIN [DB].[dbo].[Table3] AS MI WITH (NOLOCK) ON TA.coulmn10 = MI.[coulmn3]
LEFT JOIN [DB].[dbo].[Table4] AS D WITH (NOLOCK) ON TA.coulmn25 = D.coulmn1
INNER JOIN [DB1].[dbo].[Table5] AS UC WITH (NOLOCK) ON TA.coulmn12 = UC.coulmn3
WHERE (TA.[coulmn23]= '0')
答案 1 :(得分:0)
应该工作:
SELECT
TA.column1
,D.column2
,D.column3
,TA.column4
,MI.[column5]
,MI.[column6]
,D.[column7]
,D.[column8]
,TA.[column9]
,TA.[column10]
,UC.[column11]
,UC.[column12]
,TA.[column13]
,TA.[column14]
,TA.[column15]
,TA.[column16]
,TA.[column17]
,'0' AS column18
,TA.[column19]
,'12.2' AS column20
,'None' AS column21
,'0' AS column22
,'1' AS column23
,TA.[column24]
,'0' AS column25
,'0' AS column26
,LD.column27
FROM [DB].[dbo].[Table1] AS TA WITH (NOLOCK)
FULL OUTER JOIN
[DB].[dbo].[Table2] AS LD WITH (NOLOCK)
ON
TA.[coulmn1]=LD.[coulmn5]
INNER JOIN
[DB].[dbo].[Table3] AS MI WITH (NOLOCK)
ON
TA.coulmn10 = MI.[coulmn3]
LEFT JOIN
[DB].[dbo].[Table4] AS D WITH (NOLOCK)
ON
TA.coulmn25 = D.coulmn1
INNER JOIN
[DB1].[dbo].[Table5] AS UC WITH (NOLOCK)
ON
TA.coulmn12 = UC.coulmn3
WHERE
(LD.coulmn5 is null)
AND
(TA.[coulmn23]= '0')