mysql 合并表的資料

來源:互聯網
上載者:User
有2張表 每張表大概1500W的資料
表一欄位:
id pid name....

表二欄位:
id pid age....
現在要將表的資料合併了

id pid name age...

怎麼樣實現比較好

回複內容:

有2張表 每張表大概1500W的資料
表一欄位:
id pid name....

表二欄位:
id pid age....
現在要將表的資料合併了

id pid name age...

怎麼樣實現比較好

假設age這張表的pid是這樣的:

1  2 3 4 5 6 7 ...

然後name這張表的pid是這樣的:

1 3 5 6 ...

即name.pid (foreign key references on age.pid)

我們可以嘗試下面這條SQL語句:

SELECT * FROM name RIGHT OUTER JOIN age ON name.pid = age.pid ;

寫sql合并就行了, 不知道 兩個表的 id 和pid相同不

還有合并的時候是線上上?還是線下?需要保證資料庫還能對外提供服務麼

如果只保留一個id,以保留t1表為例
insert into t3(id,pid,name,age) select t1.id,t1.pid,t1.name,t2.age from t1,t2 where t1.pid = t2.pid;

如果兩個id都要保留
insert into t3(id1,id2,pid,name,age) select t1.id,t2.id,t1.pid,t1.name,t2.age from t1,t2 where t1.pid = t2.pid;

insert into t3(pid,name,age) select t1.pid,t1.name,t2.age from t1 left join t2 on t1.pid = t2.pid
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.