更新表中另一个表中的条件

时间:2016-04-20 06:30:20

标签: php mysql codeigniter

我有2个名为po_headpo_detail的表。

po_head存储有关PO Status = OPEN (qty remain > 0) / CLOSED (qty remain = 0)

的信息

po_detail存储有关订购的商品数量和订购的库存的信息 它们都与po_invoice ...

有关

我需要将po_status中的po_header更新为CLOSED IF qty remain in po_detail = 0

请注意:
qty remain = sum(po_detail_qty) from po_detail pd where pd.po_invoice = ph.po_invoice

在我在控制器中创建一些函数之后:

    function receive(){
    $data       = array();
    $subtotal = 0;
    $total = 0;
    $idnr       = $this->input->post('idtut');
    $id_detail  = $this->input->post('prDetailKodeBarang');
    $merk       = $this->input->post('prDetailMerkId');
    $kategori   = $this->input->post('prDetailKategoriId');
    $qty_receive= $this->input->post('prDetailQtyRcv');
    $price      = $this->input->post('prDetailPrice');
    $podeid     = $this->input->post('poDetailId');
    for($i = 0; $i < count($merk); $i++){
        $subtotal = $qty_receive[$i] * $price[$i];
        $data[] = array(
                        'pr_po_detail_id'       =>  $podeid[$i],
                        'pr_kode_barang'        =>  $id_detail[$i],
                        'pr_merk_item_id'       =>  $merk[$i],
                        'pr_invoice'            =>  $this->input->post('prheadInvoice'),
                        'pr_kategori_item_id'   =>  $kategori[$i],
                        'pr_detail_qty_receive' =>  $qty_receive[$i],
                        'pr_detail_price'       =>  $price[$i],
                    );
        $total += $subtotal;

        $update['po_detail_qty'] = $this->model_po->getKurangStok($podeid[$i],$qty_receive[$i]);
        $key['po_detail_id'] = $podeid[$i];
        $this->model_po->updateData('po_detail',$update,$key);
    }
    $this->db->insert_batch('pr_detail', $data);
    $head = array(
                'pr_invoice'        => $this->input->post('prheadInvoice'),
                'pr_po_invoice'     => $this->input->post('idtut'),
                'pr_pengirim'       => $this->input->post('prPengirim'),
                'pr_total_bayar'    => $total,
                'pr_date'           => $this->input->post('prHeadDate'),
                'pr_receiver'       => $this->input->post('prHeadCreator'),
                'pr_status_payment' => $this->input->post('prStatusPayment')
                );
    $this->model_po->create('pr_head', $head);
    $this->model_po->updateStatusPO($idnr);
    redirect('po');
}

这是我更新状态PO的模型:

    function updateStatusPO($idnr){
    $query = (" UPDATE po_head ph SET ph.po_status_po = 'CLOSED' WHERE
                (SELECT SUM( pd.po_detail_qty ) FROM po_detail pd WHERE pd.po_invoice =  '".$idnr."') = '0'
                AND ph.po_invoice =  '".$idnr."'
            ");
}

PO时状态po_detail_qty was "0"未发生变化。

2 个答案:

答案 0 :(得分:0)

我使用了更新加入。 请试试这个:

UPDATE po_head ph 
inner join po_detail pd on pd.po_invoice = ph.po_invoice
SET ph.po_status_po = 'CLOSED' WHERE 
                SUM( pd.po_detail_qty ) = 0
                AND ph.po_invoice =  '".$idnr."'

答案 1 :(得分:0)

在我尝试了一百次后,错误是我没有执行此查询

我在$ query之后添加 $ this-&gt; db-&gt;查询($ query); 。一切都运行良好。谢谢你