【问题标题】:codeigniter where in clause show result by orcodeigniter where in 子句通过 or 显示结果
【发布时间】:2016-06-27 04:29:38
【问题描述】:

我在codeigniter中使用'where in'时遇到了一些问题,我想修改子句不使用and而是使用or。

这是代码:

$this->db->where_in('id','1,2,3');

它是这样工作的:

$this->db->where('id',1);
$this->db->where('id',2);
$this->db->where('id',3);

但我想要这样的结果:

$this->db->where('id',1);
$this->db->or_where('id',2);
$this->db->or_where('id',3);

但它仍然使用 where in not where。谢谢

【问题讨论】:

  • 添加你正在使用的所有代码。
  • 请发布您想要得到的结果。你得到什么结果。还张贴您拥有的示例表。

标签: php mysql database codeigniter


【解决方案1】:

您的where_in() 用法错误。您必须在 array 中声明要比较的值,如下所示:

$ids = array('1', '2', '3');
$this->db->where_in('id', $ids);
//Produces: WHERE id IN ('1', '2', '3')

或者像这样:

$this->db->where_in('id', array('1', '2', '3'));
//Produces: WHERE id IN ('1', '2', '3')

【讨论】:

    【解决方案2】:

    用这个怎么样:

    $id = array('1', '2', '3');
    $this->db->or_where_in('id', $ids);
    

    更新:

    $this->db->select()->from('tbl')
       ->group_start()
         ->where('id', 1)
         ->or_group_start()
           ->where('id', 2)
           ->where('id', 3)
         ->group_end()
       ->group_end();
    

    【讨论】:

    • 它工作,但是当只有一个条件并且条件与数据不匹配时,结果仍然显示
    【解决方案3】:

    请检查 where_in 的语法

    $this->db->where_in()
    

    生成一个WHERE field IN (item, item) SQL 查询,如果合适,用 AND 连接

    $ids = array(1, 2, 3);
    $this->db->where_in('id', $ids);
    
    
    $this->db->or_where_in()
    

    生成一个WHERE field IN (‘item’, ‘item’) SQL 查询,如果合适,用 OR 连接

    $ids = array('1', '2', '3');
    $this->db->or_where_in('id', $ids);
    
    
    $this->db->where_not_in()
    

    生成一个WHERE field NOT IN (‘item’, ‘item’) SQL 查询,如果合适的话,用 AND 连接

    $ids = array('1', '2', '3');
    $this->db->where_not_in('id', $ids);
    
    
    $this->db->or_where_not_in()
    

    生成一个WHERE field NOT IN (‘item’, ‘item’) SQL 查询,如果合适的话用 OR 连接

    $ids = array('1', '2', '3');
    $this->db->or_where_not_in('id', $ids);
    

    更多信息请参考官方文档https://www.codeigniter.com/userguide3/database/query_builder.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      • 2011-03-05
      • 2015-08-06
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多