我有table1Name,填充了数据,table2没有填充数据.
select * from [database1Name].dbo.table1Name
join [database1Name].dbo.table2Name
on [database1Name].dbo.table1Name.fieldName like value;
Run Code Online (Sandbox Code Playgroud)
运行上面的sql语句后,它会加入表,但不会显示表'table1Name'中的任何填充数据.
为什么会这样?
使用JOIN它INNER JOIN意味着它将只获取条件匹配的数据.因此,如果第二个表没有数据,则永远不会满足条件,因此您不会得到任何数据.
在你的情况下,你需要一个LEFT JOIN.这将获得左表中的所有行(在您的情况下为table1Name)以及满足条件时右表中的相应值.
SELECT *
FROM [database1Name].dbo.table1Name
LEFT JOIN [database1Name].dbo.table2Name
ON [database1Name].dbo.table1Name.fieldName like [database1Name].dbo.table2Name.fieldName;
Run Code Online (Sandbox Code Playgroud)
只是提到使用连接意味着您可能会从特定表中多次获得一行.例如,由于您LIKE有条件,如果fieldName表1与表2中的2行中的fieldName匹配,那么您将得到两行,其中包含表1中的相同行和表2中的两行:
例:
表格1
FieldName
1
2
Run Code Online (Sandbox Code Playgroud)
表2
FieldName OtherField
1 1
1 2
Run Code Online (Sandbox Code Playgroud)
LEFT JOIN的结果
T1FieldName T2FieldName T2OtherField
1 1 1
1 1 2
2 NULL NULL
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |