【问题标题】:query in codeigniter: get where or在 codeigniter 中查询:获取 where 或
【发布时间】:2016-03-20 07:01:29
【问题描述】:

如何选择多个或在 codeigniter 中?

例如,select * from bla where id = 1 or id = 2 or id = 3?

如果我使用这个:

$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status); 

它是与...

【问题讨论】:

  • 请在发布问题之前搜索此网站和/或参考the manual

标签: mysql codeigniter


【解决方案1】:

您可以使用where_in 方法作为同一列的多个或语句的快捷方式:

$available_ids = [1, 2, 3];

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

如果您要检查多个列(名称为“Adam”或标题为“Grand Poobah”或状态为“Active”),您可以改用 or_where 方法:

$this->db->where('name', $name);
$this->db->or_where('title', $title);
$this->db->or_where('status', $status); 
// WHERE name = 'Adam' OR title = 'Grand Poobah' OR status = 'Active'

综合起来,你会

$available_ids = [1, 2, 3];

$query = $this->db->select('*')->from('bla')->where_in('id', $available_ids)->get();
// SELECT * FROM bla WHERE id IN (1, 2, 3)

CodeIgniter v3 Reference
CodeIgniter v2 Reference

【讨论】:

  • 但是如何在第一个选项中选择表格?
  • 您将使用$this->db->from($table_name) 方法;我已经更新了我的答案,将所有内容放在一起。
【解决方案2】:

试试这个

$array = array(
    'name' => $name, 
    'title' => $title, 
    'status' => $status
);
$this->db->where($array); 

将所有数据添加到数组中。然后将数组传递给 where 子句。

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2014-05-15
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多