mysql 對比兩個表的一致性

木子小僧發表於2017-03-30

-- A和B兩個表 資料是否一致
select 自定義
from A left join B on A.id = B.id
where B.id is null

註釋:這樣查詢的結果是A表中有而B表中沒有的資料


select 自定義
from B left join A on A.id = B.id
where A.id is null

註釋:這樣查詢的結果是B表中有而A表中沒有的資料

可以使用一個sql完成

select 自定義
from A left join B on A.id = B.id
where B.id is null
union all
select 自定義
from B left join A on A.id = B.id
where A.id is null

如果返回結果為空則表示 兩邊資料一致

相關文章