2016-11-20 126 views
0

現在我試圖將來自兩個不同表格的信息合併到一個新表格中。在將表格插入新表格時遇到問題。

這是我現在擁有的。

insert into invoices_paid(InvoiceNumber,InvoiceDate,InvoiceTotal) 
select InvoiceNumber,InvoiceDate,InvoiceTotal 
from Invoices 
insert into invoices_paid(AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription) 
select AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription 
From InvoiceLineItems 

要幫助的圖片。 Query

我需要紅色框中的信息來匹配它們各自的信息和左邊的列。我很新,但我認爲我很接近。誰能幫忙?

+0

該截圖看起來非常像SQL Server Management Studio。標記錯誤的RDMS? – Parfait

回答

0

加入InvoiceNumber上的Invoices和InvoiceLineItems表,並將其用作INSERT語句的SELECT子句的數據源。如:

insert into invoices_paid (InvoiceNumber,InvoiceDate,InvoiceTotal, 
    AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription) 
select 
    inv.InvoiceNumber,InvoiceDate,InvoiceTotal, 
    AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription 
from 
    Invoices as inv 
    inner join InvoiceLineItems as li on li.InvoiceNumber = inv.InvoiceNumber; 
0

可以使用select來找到其他的表,你也considring你必須做出一個條件,並考慮根據你的條件將兩個表中的值,在新表中插入值使行:

insert into invoices_paid(InvoiceNumber,InvoiceDate 
,InvoiceTotal,AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription) 
values (select i.InvoiceNumber,i.InvoiceDate,i.InvoiceTotal 
, b.AccountNo, b.InvoiceLineItemAmount, b.InvoiceLineItemDescription 
from Invoices as i join InvoiceLineItems 
as b where i.InvoiceNumber = b.AccountNo) 
相關問題