SQL Server比较具有相同列的两个表行,并返回更改的列 [英] SQL Server compare two tables rows with identical columns and return changed columns

查看:367
本文介绍了SQL Server比较具有相同列的两个表行,并返回更改的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较具有相同列的两个表:




  • 产品 - Id,名称,说明

  • Temp_Product - Id,Name,Description



用户现在更新的内容将被保存到 Temp_Product 。当管理员将看到该产品的详细信息,我需要显示用户做的更改。我想比较两个表与一个查询和返回列从产品更改为 Temp_Product



请向我推荐更好的方法吗?

解决方案

p> 选择p.id,p.name为orgn,t.name为altn,p.descripion为orgd,t.description为altd
来自产品p
join tmp_product t
on t.id = p.id和(t.name<> p.name或t.description<> p.description)


I want to compare two tables with the same columns:

  • product - Id, Name, Description
  • Temp_Product - Id, Name, Description

Now update done by user will be saved into Temp_Product. When admin will see the details of that product I need to show the changes done by user. I want to compare both tables with a query and return columns that have changed from Product to Temp_Product.

Please suggest me better way to do this?

解决方案

Select p.id,p.name as orgn,t.name as altn,p.descripion as orgd,t.description as altd
from product p
join tmp_product t
on t.id=p.id and (t.name<>p.name or t.description <> p.description)

这篇关于SQL Server比较具有相同列的两个表行,并返回更改的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆