How does MySQL merge two data tables with the same structure?

Source: Internet
Author: User
At present, I have two data sheet structure is the same, now I want to merge two tables into a table, but in the phpmyadmin it seems not to find how to merge the options, the Web also search some merged SQL statements, but after the execution of the error, do not know what the reason, there is no other way to achieve

INSERT INTO order_zong (Ptname, Name, Mobile, orderdate, orderprice, ordertime) VALUES ( SELECT Ptname, Name, Mobile, orderdate, orderprice, ordertime from hnb);

The above code error 12 errors, probably the way is not right, trouble master to give a simple way

Reply content:

At present, I have two data sheet structure is the same, now I want to merge two tables into a table, but in the phpmyadmin it seems not to find how to merge the options, the Web also search some merged SQL statements, but after the execution of the error, do not know what the reason, there is no other way to achieve

INSERT INTO order_zong (Ptname, Name, Mobile, orderdate, orderprice, ordertime) VALUES ( SELECT Ptname, Name, Mobile, orderdate, orderprice, ordertime from hnb);

The above code error 12 errors, probably the way is not right, trouble master to give a simple way

If the data structure is the same, the following can be:

INSERT INTO `your_table_name`SELECT *FROM `destination_table_name`;

INSERT into C Select from a UNION select from b

Export SQL Merge after import haha

Insert into Table2(field1,field2,...) select value1,value2,... from Table1

INSERT IGNORE INTO order_zong (Ptname, Name, Mobile, orderdate, orderprice, ordertime)        SELECT Ptname, Name, Mobile, orderdate, orderprice, ordertime from hnb;

If the table is re-inserted, insert into will not use values, but direct select table.
Note: You may have an error when inserting a primary key that repeats itself.

Can you put out the mistake? Will some fields define a unique. So the import uniqueness conflict is wrong.

or create a new table to merge
If you need to go heavy, use union.
create table new_table SELECT Ptname, Name, Mobile, orderdate, orderprice, ordertime from order_zong union all SELECT Ptname, Name, Mobile, orderdate, orderprice, ordertime from hnb

  • 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.