2014-05-05 31 views
0

我有以下查询产生的结果如下表,MySQL查询插入行到表不列中有一个特定的值

select i.invoice_date,i.invoice_number,i.invoice_subtotal,p.payment from 
invoice i 
inner join payments p 
on i.invoice_number = p.invoiceGRN_id 
where i.payment_type = 'Credit' 

enter image description here

我需要做的是,

  • 插入一行每个INVOICE_NUMBER有关 invoice_subtotal(invoice_subtotal),这是同特定 INVOICE_NUMBER,
  • 与支付= 0.0其中如果特定INVOICE_NUMBER不 已经有一个记录付款= 0.0

请在这方面帮助我。

回答

0

我假设你想在2个表(发票和付款)插入,你需要用以下2个查询程序,

insert into invoice(invoice_date, invoice_number, invoice_subtotal,...) 
select invoice_date, invoice_number, invoice_subtotal,... from invoice i1 where i1.payment_type = 'Credit' and not exists(select * from payments p1 where i1.invoice_number = p1.invoiceGRN_id and p1.payment = 0.0); 

insert into payments(invoiceGRN_id, payment, ...) 
select invoiceGRN_id, payment,... from payments p1 where p1.invoiceGRN_id not in (select invoiceGRN_id from payments p2 where p2.payment=0.0); 
相关问题