如何借助 MySQL 连接实现表之间的差异?
我们可以通过将从第一张表到第二张表以及从第二张表到第一张表的并集排除连接来获取表之间的差异。为了理解这一点,我们以下面这两张表为例 −
mysql> Select * from value1; +-----+-----+ | i | j | +-----+-----+ | 1 | 1 | | 2 | 2 | +-----+-----+ 2 rows in set (0.00 sec) mysql> Select * from value2; +------+------+ | i | j | +------+------+ | 1 | 1 | | 3 | 3 | +------+------+ 2 rows in set (0.00 sec)
现在,以下查询将得出表‘value1’和‘value2’之间的差异 −
mysql> Select * from value1 left join value2 using(i,j) where value2.i is NULL UNION Select * from value2 left join value1 using(i,j) Where value1.i is NULL; +------+-----+ | i | j | +------+-----+ | 2 | 2 | | 3 | 3 | +-----+------+ 2 rows in set (0.07 sec)
广告