在codeigniter中查询:获取位置或

时间:2015-12-14 17:29:25

标签: mysql codeigniter

如何选择多个或在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); 

与AND ...

2 个答案:

答案 0 :(得分:0)

您可以使用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

答案 1 :(得分:0)

试试这个

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

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