【问题标题】:CodeIgniter Delete where condition and where not in given arrayCodeIgniter 删除给定数组中的条件和位置
【发布时间】:2018-01-10 21:07:42
【问题描述】:

如何在 Codeigniter 中运行删除查询。我有一组数组和一个键。在 MySQL 中,我的查询运行完美。

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)

在 Codeigniter 中如何编写和运行这个查询。我尝试了下面的代码,但没有工作。

$ids = '5,9,12';
$this->db->where('style_id', $style_id)->where_not_in('ID', $ids);
$this->db->delete('TABLE');

【问题讨论】:

  • 请告诉我 $id 的值。

标签: php mysql codeigniter activerecord query-builder


【解决方案1】:

试试这个:你必须有 $ids 作为数组

$ids = array(5,9,12);
$style_id= 2;


$this->db->where('style_id', $style_id);
$this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');

【讨论】:

  • 请删除$this->db->->where_not_in('ID', $ids);中多余的->
  • 我试过了,但它删除了整条记录。请检查我的 mysql 查询。
  • 那么你想尝试删除什么?
  • 谢谢。我弄错了。问题是,我有字符串。
  • 为什么会被否决? @Downvoter 你能解释一下吗?这样我就可以纠正我的错误
【解决方案2】:

如果你想这样,你可以写成一行:

$this->db->delete('table name',array('field1'=>'value1','field2 !='=>'value2'));

简短而简单。

【讨论】:

    【解决方案3】:

    试试这个

    this->db->where('style_id', $style_id);
    this->db->where_not_in('ID', $ids);
    $this->db->delete('TABLE');
    

    这将使用AND 子句连接两个参数


    数据应该是

    数组

    $ids = array(10, 20, '30);
    

    身份证

    $style_id = 15;
    

    阅读Looking for Specific Data in codeigniter.com

    【讨论】:

    • $this->db->where('style_id', $style_id)->where_not_in('ID', $ids); 这将使用OR 子句对数据进行投标
    • 我试过了,但它删除了整条记录。请检查我的 mysql 查询。
    • 你想做什么?请用各自的数据解释一下
    【解决方案4】:

    你可以直接运行这个:

    DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)
    in $this->db->query("DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)");
    

    【讨论】:

      【解决方案5】:

      // 试试看

      $ids = array(5,9,12);
      $style_id =2;
      $this->db->where('ID', $style_id);
      $this->db->where_not_in('ID', $ids);
      $this->db->delete('TABLE');
      

      【讨论】:

      • 我试过了,但它删除了整条记录。请检查我的 mysql 查询。
      • 再次检查@BhuneshSatpada
      猜你喜欢
      • 2014-09-07
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      相关资源
      最近更新 更多