有没有一种快速方法可以在 SQL 中比较两个格式相同的表?
我想做一个 SQL 查询来比较两个具有相同列(名称和类型)的表。每个表都有一个唯一的键。我希望查询返回包含不相等值的任何行。我知道可以做这样的事情
select *
from table_1, table_2
where
table_1.key = table_2.key
and (
table_1.col1 != table_2.col1 OR
table_1.col2 != table_2.col2 OR
...
)
,但这会很乏味,因为有大量且可能可变的列数。
编辑
如果有帮助,我正在使用 tsql 系统。
I would like to make an SQL query to compare two tables with identical columns, both names and types. Each table has a unique key. I want the query to return any rows that contain unequal values. I know could do something like this
select *
from table_1, table_2
where
table_1.key = table_2.key
and (
table_1.col1 != table_2.col1 OR
table_1.col2 != table_2.col2 OR
...
)
but this would be tedious since there are a large and potentially variable number of columns.
edit
If it helps, I'm using a tsql system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定您使用的数据库类型,但如果您使用 SQL Server 2005 或更高版本,请尝试以下操作:
Not sure what type of DB you are using but if you are using SQL Server 2005 or higher try this:
这怎么办..
How abt this..
通过 SQL Server 验证:
通过 Oracle 验证:
Verified with SQL Server:
Verified with Oracle: