Sql 连接没有空值的表
我不确定如何解释我需要什么,但以下是数据:Sql 连接没有空值的表,sql,sql-server,Sql,Sql Server,我不确定如何解释我需要什么,但以下是数据: Table 1 District -1 3 2 1 3 Table 2 ID ID_Name 1 Main 1 2 Main 2 3 Main 3 我如何连接这些表,使其看起来像这样 District -1 Main 3 Main 2 Main 1 Main 3 我假设第二列为此命名为Name,但您可以使用COALESCE和LEFT JOIN: Select Coalesc
Table 1
District
-1
3
2
1
3
Table 2
ID ID_Name
1 Main 1
2 Main 2
3 Main 3
我如何连接这些表,使其看起来像这样
District
-1
Main 3
Main 2
Main 1
Main 3
我假设第二列为此命名为
Name
,但您可以使用COALESCE
和LEFT JOIN
:
Select Coalesce(T2.Name, Str(T1.District)) As District
From Table1 T1
Left Join Table2 T2 On T1.District = T2.Id
我假设第二列为此命名为
Name
,但您可以使用COALESCE
和LEFT JOIN
:
Select Coalesce(T2.Name, Str(T1.District)) As District
From Table1 T1
Left Join Table2 T2 On T1.District = T2.Id
假设表2有
Table 2
ID col2
1 Main 1
2 Main 2
3 Main 3
你可以使用左连接
select table1.Distric, table2.col2
from table1
left join table2 on table1.dictrict = t2.ID
order by table2 col2
假设表2有
Table 2
ID col2
1 Main 1
2 Main 2
3 Main 3
你可以使用左连接
select table1.Distric, table2.col2
from table1
left join table2 on table1.dictrict = t2.ID
order by table2 col2
您可以使用左联接:
Select coalesce(t2.col, t1.District) from table1 t1
left join table2 t2 on t1.District = t2.Id
您可以使用左联接:
Select coalesce(t2.col, t1.District) from table1 t1
left join table2 t2 on t1.District = t2.Id
您能发布您到目前为止尝试过的内容吗?
code left OUTER JOIN Pubworks.dbo.District ON Pubworks.dbo.District.ID=Pubworks.dbo.csc.DistID
Results:District NULL Main 3 Main 2 Main 1 Main 3
花点时间理解外部联接:左、右、,和完整外部。您可以发布您到目前为止尝试过的内容吗?code left-OUTER连接Pubworks.dbo.District.ID=Pubworks.dbo.csc.DistID
结果:District-NULL-Main 3 Main 2 Main 1 Main 3
花点时间了解外部连接:左、右和完整外部。