2014-05-05 29 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); 
相關問題