2013-07-09 95 views
0

这是我的表记录:删除与WHERE子句

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'); 

    } 

} 

是否有任何其他简单的方法来做到这一点?

+0

如果你想删除记录peter和sammy,那么为什么有一个循环和'$ pick'中有什么? –

回答

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子句匹配的数据。

+0

非常感谢这对我有用。 – user2046410