将多个删除查询修改为尽可能少的更新查询?
Posted
技术标签:
【中文标题】将多个删除查询修改为尽可能少的更新查询?【英文标题】:Modify multiple delete queries into as little update queries as possible? 【发布时间】:2012-11-27 10:48:28 【问题描述】:我们目前有这个代码sn-p:
$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
列。
我知道我可以修改它。保留相同数量的查询,但我希望作为更新而不是删除,我可以将其缩小为更少的查询?
【问题讨论】:
【参考方案1】:如果您担心删除查询的数量,您可以使用多表删除查询,通过它可以将其减少为单个查询DELETE Syntax
UPDATE
也可以实现多表查询UPDATE Syntax
【讨论】:
以上是关于将多个删除查询修改为尽可能少的更新查询?的主要内容,如果未能解决你的问题,请参考以下文章