1. 程式人生 > >MySQL#利用子查詢更新

MySQL#利用子查詢更新

MySql利用子查詢更新


示例1

update t_pandora_bill_summary as ua
        inner join
    (select 
        g1.shop_id, sum(g1.order_amt) repair_amount
    from
        (select 
        a.shop_id,
            a.order_id,
            (a.order_amount + a.shipping_fee) order_amt,
            a.order_state,
            a.order_status,
            a.order_mtime,
            a.order_ctime
    from
        t_pandora_order a
    where
        a.order_state = 3
            and a.order_mtime >= '2015-02-06 14:21:00'
            and a.order_mtime <= '2015-02-07 22:22:00') g1
    group by g1.shop_id) ub ON ua.shop_id = ub.shop_id 
set 
    ua.bill_total = ua.bill_total + ub.repair_amount,
    ua.bill_balance = ua.bill_balance + ub.repair_amount
where
    ua.shop_id = '';

示例2

update t_pandora_settlement as a
        inner join
    (select 
        b1.parent_id,
            sum(b1.amt_goods + b1.amt_shipping_fee) amt_receivables,
            sum(b1.amt_commission + b1.amt_other) amt_payable
    from
        t_pandora_settlement_item b1
    where
        b1.parent_id = '@parentId'
    group by b1.parent_id) b ON a.id = b.parent_id 
set 
    a.amt_receivables = b.amt_receivables,
    a.amt_payable = b.amt_payable
where
    a.shop_id = '@shopId';