我想在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语句中的错误?谢谢。
答案 0 :(得分: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
答案 1 :(得分: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();
干杯,
答案 2 :(得分: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)
}
}
答案 3 :(得分:0)
是activerecord有问题使用或,如果你使用activerecord它不会添加开始和结束括号或条件然后比较和你的查询将是这样的
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);
答案 4 :(得分: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');
希望它会对你有所帮助。