0

我想在 CodeIgniter 中实现以下 mysql 语句。

select * from table_name 
         where deleted = 0 and (id = "18" or name = "efgh" or imei = "1244");

我在 CodeIgniter 中编写了以下语句:

$this->db->where('deleted', 0);
$this->db->or_where('id', $this->table_name->id);
$this->db->or_where('imei', $this->table_name->imei);
$this->db->or_where('name', $this->table_name->name);
$result= $this->db->get('table_name')->result();

但是这两个语句给出了不同的输出。谁能指出 CodeIgniter 语句中的错误?谢谢你。

4

5 回答 5

1

请使用这个:-

   $where = "deleted='0' AND (id='18' OR name='efgh' OR imei='1244')";
   $this->db->where($where);
   $result= $this->db->get('table_name')->result();

有关更多详细信息,您可以访问:-

http://ellislab.com/codeigniter/user-guide/database/active_record.html

于 2013-07-25T07:37:56.767 回答
1

我会用

$this->db->where('deleted', 0);
$this->db->where("(id='18' OR name='efgh' OR imei='1244')", NULL, FALSE);
$result = $this->db->get('table_name')->result();

干杯,

于 2013-07-25T17:17:49.310 回答
0

我以前也遇到过同样的问题。

我无法弄清楚$this->db功能。

我修改system/database/DB_active_rec.php _compile_select()功能

原文: 1746行

if (count($this->ar_like) > 0) {
  if (count($this->ar_where) > 0) {
    $sql .= "\nAND ";
  }
  $sql .= implode("\n", $this->ar_like);
}

我的变化:

if (count($this->ar_like) > 0) {
  if (count($this->ar_where) > 0) {
    $sql .= "\nAND ("; // open parenthesis for where and like command
  }
  $sql .= implode("\n", $this->ar_like);
  if (count($this->ar_where) > 0) {
    $sql .= "\n)"; // close parenthesis and sample sql text > WHERE column = x AND (LIKE QUERIES)
  }
}
于 2013-07-25T07:36:20.423 回答
0

是的,activerecord 使用有问题,或者,如果您使用 activerecord,它将不会为 or 条件添加开始和结束括号,然后进行比较,您的查询将是这样的

select * from table_name where deleted = 0 and id="18" or name = "efgh" or imei = "1244";

使用它

$where = 'deleted = 0 and (id="18" or name = "efgh" or imei = "1244")';

$this->db->where($where);
于 2013-07-25T07:39:53.243 回答
0
There is two way to do the same thing.
first 
$condition = "deleted='0' AND (id='18' OR name='efgh' OR imei='1244')";
$this->db->where($condition);
$result= $this->db->get('table_name')->result();
 and 
write your costume query 

$this->db->query('YOUR QUERY HERE');

希望它会帮助你。

于 2013-07-25T07:43:40.353 回答