MySQL數據表合並(兩表字段相同)以及數據去重(抄)


數據去重
現有兩個表 test_01 test_02 其中test_01有重復數據

統計重復數據
select count(*) as repeat_count,name from test_01 group by name having repeat_count > 1;
1


使用DISTINCT關鍵字過濾重復數據
select distinct name,age from test_01;
1


也可以使用GROUP BY過濾重復數據
select name,age,gender from test_01 group by name;
1


刪除重復的數據,采用create table select方式從以上過濾完數據的查詢結果中創建新表,作為臨時表,然后把原來的表drop刪除,再把臨時表重命名為原來的表名
create table test_temp select name,age,gender from test_01 group by name;
drop table test_01;
alter table test_temp rename to test_01;
1
2
3


這樣便得到了無重復數據的 test_01


合並test_01 test_02(兩表結構相同)采用暴力添加數據的方法,這里把test_02 表的數據合並到test_01表
insert into test_01(name,age,gender) select name,age,gender from test_02;
1


得到合並后的test_01

————————————————
版權聲明:本文為CSDN博主「metoo9527」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/metoo9527/article/details/80085128


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM