Mysql MERGE table data

Source: Internet
Author: User
There are two tables, each of which contains about data table fields: idpidname .... table 2 field: idpidage .... now we want to merge the table data with idpidnameage... how can we achieve better results? There are two tables, each of which has approximately data records?
Table field:
Id pid name ....

Table 2 fields:
Id pid age ....
Now we want to merge the table data.

Id pid name age...

Better implementation

Reply content:

There are two tables with about data per table
Table field:
Id pid name ....

Table 2 fields:
Id pid age ....
Now we want to merge the table data.

Id pid name age...

Better implementation

Assume that the pid of the table "age" is as follows:

1  2 3 4 5 6 7 ...

Then the pid of the name table is as follows:

1 3 5 6 ...

That isname.pid (foreign key references on age.pid)

We can try the following SQL statement:

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

Just write SQL merge. I don't know if the IDs and PIDs of the two tables are the same.

Is the merge online? Or offline? Must the database provide external services?

If only one id is retained, table t1 is retained as an example.
Insert into t3 (id, pid, name, age) select t1.id, t1.pid, t1.name, t2.age from t1, t2 where t1.pid = t2.pid;

If both IDs must be retained
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.