现在我正在尝试将两个不同表中的信息连接到一个新表中。
这就是我现在所拥有的。
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 :(得分: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;
答案 1 :(得分:0)
你可以使用select在新表中插入值来查找你要考虑的其他表中的值,你也必须考虑条件,并考虑根据你必须创建行的条件连接两个表:
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 )