简体   繁体   中英

How to merge two tables with same values in a column

I have two tables, Table1 and Table2.

Table1 has the columns " ID "," Date " and Table2 has the columns " ID ", " Cost ".

Now, the way I want to merge these two tables into a new table Table3 with columns " ID ", " Date ", " Cost " is that the Cost and Date are in the same row which have the same ID in both Table2 and Table1 respectively.

In short, I want to glue the two tables with respect to a column, in this case " ID ".

I've looked into statements like INSERT INTO TABLE but I haven't been able to get it to work.

You can perform an insert-select on the result of the join between the two source tables:

CREATE TABLE table3 AS
SELECT table1.id AS id, date, cost
FROM   table1
JOIN   table2 ON table1.id = table2.id

please use below SQL to merge

insert into table3 (id, date1, cost) select a.id, b.date1, a.cost from table1 a, table2 b where a.id=b.id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM