2012-11-27 22 views
0

目前,我們有這樣的代碼片段:將多個刪除查詢修改爲儘可能少的更新查詢?

$this->db->where('id', $id)->delete($this->table); 
$this->db->where('product_id', $id)->delete($this->clink); 
$this->db->where('product_id', $id)->delete($this->tlink); 

$variants = $this->db->select('id')->from($this->vlink)->where('product_id', $id)->get(); 
if ($variants->num_rows() !== 0) { 

    //turns array(array('id' => 1), array('id' => 2)) into array(1,2) 
    $variants = $variants->result_array(); 
    foreach ($variants as &$v) { 
     $v = $v['id']; 
    } 

    $this->db->where('product_id', $id)->delete($this->vlink); 
    $this->db->where_in('variant_id', $variants)->delete('cart_variant_values'); 
    $this->db->where_in('variant_id', $variants)->delete('cart_variant_images'); 
    $this->db->where_in('variant_id', $variants)->delete('cart_variant_documents'); 
} 

換算成這樣:

delete from $this->table where id = $id 
delete from $this->clink where product_id = $id 
delete from $this->tlink where product_id = $id 

$variants = select id from $this->vlink where product_id = $id //e.g. (1,2) 

delete from $this->vlink where product_id = $id 
delete from cart_variant_values where variant_id in $variants 
delete from cart_variant_images where variant_id in $variants 
delete from cart_variant_documents where variant_id in $variants 

現在我需要修改代碼,而不是刪除所有的行,它設置對他們的end_date列所有。

我知道我可以修改它。留下它與相同數量的查詢,但我希望是一個更新而不是刪除我可以縮減成更少的查詢?

回答

0

如果你擔心刪除查詢的數量,您可以使用多個數據表刪除查詢,通過它可以將其降低到一個單一的查詢
DELETE Syntax

可以實現UPDATE
多表查詢 UPDATE Syntax