Sql 试图查找一个表中的所有列而不是另一个表中的所有列--左联接不起作用

Sql 试图查找一个表中的所有列而不是另一个表中的所有列--左联接不起作用,sql,tsql,left-join,information-schema,Sql,Tsql,Left Join,Information Schema,我试图通过在COLUMN\u NAME=COLUMN\u NAME上的Information\u schema上使用自联接,以及它自身的内部联接,来查找一个表中存在而另一个表中不存在的所有列。我似乎不明白我的逻辑有什么问题: select c.Column_name, c2.Column_name from information_schema.columns c left outer join Information_Schema.Columns c2 ON c.COLUMN_NAME =

我试图通过在COLUMN\u NAME=COLUMN\u NAME上的Information\u schema上使用自联接,以及它自身的内部联接,来查找一个表中存在而另一个表中不存在的所有列。我似乎不明白我的逻辑有什么问题:

select c.Column_name, c2.Column_name 
from information_schema.columns c
left outer join Information_Schema.Columns c2
ON c.COLUMN_NAME = c2.column_name
Where c.TABLE_NAME = 'Table1'
and c2.TABLE_NAME = 'Table2'
我应该去

Col1(a) | Col1(b)
Col2(a) | null
Col3(a) | Col3(b)
Col4(a) | Col4(b)
Col5(a) | null
但是相反,我得到了

Col1(a) | Col1(b)
Col3(a) | Col3(b)
Col4(a) | Col4(b)

这是为什么?

联接的左侧右侧表上的筛选器应仅位于on子句内。在where子句中指定它们时,左连接将自动变成内部连接

select c.Column_name, c2.Column_name 
from information_schema.columns c
left outer join Information_Schema.Columns c2
ON c.COLUMN_NAME = c2.column_name AND c2.TABLE_NAME = 'Table2'
Where c.TABLE_NAME = 'Table1'

left联接的right表上的筛选器应仅位于on子句内。在where子句中指定它们时,左连接将自动变成内部连接

select c.Column_name, c2.Column_name 
from information_schema.columns c
left outer join Information_Schema.Columns c2
ON c.COLUMN_NAME = c2.column_name AND c2.TABLE_NAME = 'Table2'
Where c.TABLE_NAME = 'Table1'