Home > Backend Development > PHP Tutorial > mysql 合并表的数据

mysql 合并表的数据

WBOY
Release: 2016-06-06 20:36:42
Original
1156 people have browsed it

有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是这样的:

<code>1  2 3 4 5 6 7 ...
</code>
Copy after login

然后name这张表的pid是这样的:

<code>1 3 5 6 ...
</code>
Copy after login

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

我们可以尝试下面这条SQL语句:

<code>SELECT * FROM name RIGHT OUTER JOIN age ON name.pid = age.pid ;
</code>
Copy after login

写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;

<code>insert into t3(pid,name,age) select t1.pid,t1.name,t2.age from t1 left join t2 on t1.pid = t2.pid
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template