![](/img/trans.png)
[英]How to merge two tables having the same column in both the tables in order to identify the discrepancies in MySQL?
[英]How to merge two tables with same values in a column
我有兩個表,Table1和Table2。
Table1具有列“ ID ”,“ Date ”, Table2具有列“ ID ”,“ Cost ”。
現在,我希望將這兩個表合並到一個新表Table3中的方式,其中列“ ID ”,“ 日期 ”,“ 成本 ”是成本和日期在同一行中,在Table2和Table1中具有相同的ID分別。
簡而言之,我想將兩個表相對於列粘合,在本例中為“ ID ”。
我已經研究過像INSERT INTO TABLE
這樣的語句但是我無法讓它工作。
您可以對兩個源表之間的連接結果執行insert-select:
CREATE TABLE table3 AS
SELECT table1.id AS id, date, cost
FROM table1
JOIN table2 ON table1.id = table2.id
請使用下面的SQL進行合並
insert into table3(id,date1,cost)從table1 a,table2 b中選擇a.id,b.date1,a.cost,其中a.id = b.id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.