SELECT ID FROM (
SELECT DISTINCT A.ID AS ID FROM TABLEA A #有ID: 1 2 3 4 5
UNION ALL
SELECT DISTINCT B.ID AS ID FROM TABLEB B #有ID: 2 3
)TEMP GROUP BY ID HAVING COUNT(ID) = 1 #合并后分组,找到个数为1的,就是差异的1 4 5
Mysql没有提供差集,和交集,
为了实现差集和交集,我们可以做合并后,group by
COUNT(ID) = 1 差集
<pre name="code" class="sql">COUNT(ID) = 2 交集