急用!怎麼比較資料庫中的兩個表中 的內容,刪除相同的資料,保留不一樣的資料

來源:互聯網
上載者:User
急用!!如何比較資料庫中的兩個表中 的內容,刪除相同的資料,保留不一樣的資料
急求!!!

請問,如何比較兩個表中的資料,一條條查。如果資料相同,查一條刪一條;如果資料不同,分別保留。

最後將兩個表中保留的不一樣的資料 輸出來並共同匯入到另外一個專門盛放錯誤的表中。 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
然後再刪除吧
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.