[英]Swap columns from two sql server tables
我想知道无论如何我可以比较SQL Server中的两列。
这两列位于两个不同的表中。
当列1的值小于列2的值时:
我想用第2列的值替换第1列的值。
update table1 t1
set t1.col1 = (select t2.col2
from table2 t2
where t2.id = t1.id
and t1.col1 < t1.col2)
这样的事情应该很容易做到。
我看到的唯一棘手的问题是将table2中的行与table1中的行匹配。 在我的例子中,我认为两个表共享一个唯一的“id”列,可以轻松匹配。 使用更合适的方法修改查询。
你应该可以做这样的事情:
update tablename set column1=column2
from table1 inner join table2 on joincondition
where column1 < column2;
没有实际的表结构,很难更具特色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.