单个查询中的Where和Or Where子句?使用codeigniter

时间:2016-02-16 13:18:54

标签: php mysql codeigniter codeigniter-3

这就是我想要实现的目标:select * from 'mytable' where id=2 and where status=hold or status=reserved or status=deleted

如何在 CodeIgniter 3.0 中执行此操作?

我在WAMP设置工作。

8 个答案:

答案 0 :(得分:3)

CodeIgniter中你可以这样使用:

$this->db->select();
$this->db->from('mytable');
$this->db->where('id',2);
$this->db->or_where('status','hold');
$this->db->or_where('status','reserved');
$this->db->or_where('status','deleted');
$query = $this->db->get();
$query->result_array(); // will return result in array format

<强> CodeIgniter User Guide

答案 1 :(得分:3)

也许这会有所帮助:

$results = $this->db->where("id",2)->where_in('status',array("hold","deleted","reserved"))->get("mytable")->result();

答案 2 :(得分:1)

CodeIgniter 中查询:

$this->db->select();
$this->db->from('tbl_name');
$this->db->where('id',2);
$this->db->or_where('status','hold');
$this->db->or_where('status','reserved');
$this->db->or_where('status','deleted');

答案 3 :(得分:1)

您可以使用$ this-&gt; db-&gt; query()来实现此目的。

 $query= $this->db->query("select * from mytable where id=2 and (status='hold' or status='reserved' or status='deleted')");
$result=$query->result_array();

答案 4 :(得分:1)

$this->db->select('*');
$this->db->from('mytable');
$this->db->where('id',2);
$this->db->where('status','hold');
$this->db->or_where('status','reserved');
$this->db->or_where('status','deleted');
$query = $this->db->get();
$query->result_array();

答案 5 :(得分:0)

正确的SQL查询是:

SELECT * FROM mytable WHERE id = '2' AND (status = 'hold' 
OR status = 'reserved' OR status = 'deleted')

答案 6 :(得分:0)

啊,啊,明白了。为了防止其他人在同样的情况下遇到困难,可以在CodeIgniter中处理它。

在CodeIgniter中,我们可以像这样手动编写自己的子句:

$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

答案 7 :(得分:-1)

您可以使用下面提到的查询 -

select * from tbl_name where id=2 and status in ('hold','reserved','deleted');