Home  >  Article  >  Backend Development  >  How to merge two data tables with the same structure in mysql?

How to merge two data tables with the same structure in mysql?

WBOY
WBOYOriginal
2016-12-01 00:25:351755browse

Currently, I have two data tables with the same structure. Now I want to merge the two tables into one table, but I don’t seem to find the option to merge them in phpmyadmin. I also searched for some merged SQL statements online, but after execution Still getting an error, I don’t know why, is there any other way to achieve it

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

The above code reports 12 errors, maybe the method is wrong at all, please give me a simple solution

Reply content:

Currently, I have two data tables with the same structure. Now I want to merge the two tables into one table, but I don’t seem to find the option to merge them in phpmyadmin. I also searched for some merged SQL statements online, but after execution Still getting an error, I don’t know why, is there any other way to achieve it

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

The above code reports 12 errors, maybe the method is wrong at all, please give me a simple solution

If the data structure is the same, the following will do:

<code>INSERT INTO `your_table_name`
SELECT *
FROM `destination_table_name`;</code>

INSERT INTO c SELECT FROM a UNION SELECT FROM b

Export the sql and merge it and then import it haha

<code>Insert into Table2(field1,field2,...) select value1,value2,... from Table1</code>

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

If you are looking up a table and then inserting it, you don’t need values ​​after insert into, but directly select the table.
Note: The error you reported may be due to duplicate primary keys during insertion

Can you post the error? Are there some fields defined as unique? So the import uniqueness conflict went wrong

Or create a new table to merge
If you need to remove duplicates, 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

Statement:
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