使用WHERE子句删除记录

时间:2013-07-09 09:28:45

标签: php codeigniter

这是我的表:

id    t_id     min      max       range      name
1       2      2         6          2        Peter
2       2      3         5          3        Leofer
3       2      3         5          4        Josh
4       2      3         5          5        Sonny
5       2      2         6          6        Sammy

我想删除PeterSammy的记录。此表中的所有内容都包含可变数据,因此使用列名称将是最佳解决方案。

到目前为止,这是我的代码:

for ($x = 0; $x <= sizeof($pick); $x++ )
{
      $where = ('pick_range' < $pick[$x] && 'pick_range' > $pick[$x] );
      $this->db->select('*');
      $this->db->where('teaser_id',$first_page_data['teaser_id']);
      $this->db->where('sb_id',$sbid);
      $this->db->where('pick_range ', $where);
      $query = $this->db->get('tbl_name');

      if($query->num_rows() == 1);
      {

        $this->db->where('pick_range',$where);
        $this->db->where('teaser_id',$first_page_data['teaser_id']);
        $query = $this->db->delete('tbl_name');

    }

}

还有其他简单方法吗?

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

for ($x = 0; $x <= sizeof($pick); $x++ )
{
    $this->db->trans_start();
    $where = ('pick_range' < $pick[$x] && 'pick_range' > $pick[$x] );
    //$this->db->select('*');
    $this->db->where('teaser_id',$first_page_data['teaser_id']);
    $this->db->where('sb_id',$sbid);
    $this->db->where('pick_range ', $where);
    //$query = $this->db->get('tbl_name');

 //if($query->num_rows() == 1);
 //{

    $this->db->where('pick_range',$where);
    $this->db->where('teaser_id',$first_page_data['teaser_id']);
    $this->db->delete('tbl_name');
    $this->db->trans_complete();
    return TRUE;

 //}

}

这将删除与WHERE子句匹配的数据。