我的问题是用SQL Server中的键替换。谁能给我一个查询来做到这一点?
感谢您的回答!
Table1
:
ID | Code | Des | more columns
---+------+-----+-------------
1 | 100 | a | ...
2 | 200 | b | ...
3 | 300 |data3| ...
Table2
:
ID | Code | Des
---+------+------
1 | 100 | data1
2 | 200 | data2
结果必须是这样的:
ID | Code | Des | more columns
---+------+-----+-------------
1 | 100 |data1| ...
2 | 200 |data2| ...
3 | 300 |data3| ...
LEFT JOIN
如果没有table2.Des值,则执行a ,取table1.Des代替:
select t1.ID, t1.Code, coalesce(t2.Des, t1.Des), t1.more Column
from table1 t1
left join table2 t2 on t1.code = t2.code
或者,也许您想要这样:
select * from table2
union all
select * from table1 t1
where not exists (select 1 from table2 t2
where t2.code = t1.code)
即返回table2行,并且如果代码在table1中但不在table2中,则还返回该行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句