Home > Backend Development > PHP Tutorial > 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
Release: 2023-03-01 16:58:01
Original
1777 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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

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

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