mysql 两表比对_比较MySQL中两个表之间的差异

在MySQL中,当遇到两个结构相同的表t1和t2可能含有不同数据时,如何进行差异比较?文章指出不能直接使用INTERSECT或MINUS操作,并分享了一个通过ROW函数模拟INTERSECT来找出不同数据的SQL查询方法,适用于不支持INTERSECT的MySQL版本。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

Suppose I have two tables, t1 and t2 which are identical in layout but which may contain different data.

What's the best way to diff these two tables?

To be more precise, I'm trying to figure out a simple SQL query that tells me if data from one row in t1 is different from the data from the corresponding row in t2

It appears I cannot use the intersect nor minus. When I try

SELECT * FROM robot intersect SELECT * FROM tbd_robot

I get an error code:

[Error Code: 1064, SQL State: 42000] You have an error in your SQL

syntax; check the manual that corresponds to your MySQL server version

for the right syntax to use near 'SELECT * FROM tbd_robot' at line 1

Am I doing something syntactically wrong? If not, is there another query I can use?

Edit: Also, I'm querying through a free version DbVisualizer. Not sure if that might be a factor.

解决方案

INTERSECT needs to be emulated in MySQL:

SELECT 'robot' AS `set`, r.*

FROM robot r

WHERE ROW(r.col1, r.col2, …) NOT IN

(

SELECT col1, col2, ...

FROM tbd_robot

)

UNION ALL

SELECT 'tbd_robot' AS `set`, t.*

FROM tbd_robot t

WHERE ROW(t.col1, t.col2, …) NOT IN

(

SELECT col1, col2, ...

FROM robot

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值