表之间的差异如何借助 MySQL 联接来实现?
我们可以通过对第 1 张表和第 2 张表进行并集排除联接以及对第 2 张表和第 1 张表进行并集排除联接来获取表之间的差异。为了理解这一点,我们将采用以下两个表的示例 −
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)
广告