急用!!如何比較資料庫中的兩個表中 的內容,刪除相同的資料,保留不一樣的資料
急求!!!
請問,如何比較兩個表中的資料,一條條查。如果資料相同,查一條刪一條;如果資料不同,分別保留。
最後將兩個表中保留的不一樣的資料 輸出來並共同匯入到另外一個專門盛放錯誤的表中。 PHP
分享到:
------解決方案--------------------
資料是否相同? 是根據某個欄位來判斷,還是資料表中每個欄位都要比較
可以查詢出來這兩個表中的所有資料,迴圈比較
------解決方案--------------------
呵呵,最簡單的做法就是查詢一個表所有資料迴圈跟另一個表的比較
還有一個就是直接聯表查詢,
比如select a,id,b.id from a,b where a.filed1=b.filed1 and a.filed2=b.filed2 and a.filed3=b.filed3
這樣查詢然後刪除了
------解決方案--------------------
1、取得 表A 的記錄數備用 select count(*) as cnt from A
2、提取出與表A不同的表B的記錄 select * from A union select * from B limit cnt
3、取得 表B 的記錄數備用 select count(*) as cnt from B
4、提取出與表B不同的表A的記錄 select * from B union select * from A limit cnt
後面怎麼做?你自己決定
------解決方案--------------------
可以考慮用預存程序
------解決方案--------------------
不要那麼麻煩了,把表發出來,
create table ..
insert into ..
然後把你想要的結果做一份範本出來
------解決方案--------------------
你應該給出測試資料
------解決方案--------------------
select a,id,b.id from a,b where a.filed1=b.filed1 and a.filed2=b.filed2 and a.filed3=b.filed3
然後再刪除吧