我正在尝试访问本地数据库(在我的虚拟机上)中的数据,并且我必须使用 CodeIgniter 的查询构建类来获取数据。我有这个查询,我已经在 sql 中弄清楚了:
select message, created
from logs
where username = 'user'
and (
created > '1487695796'
and created < '1487782196'
)
and (
message = 'login failure'
or message = 'login success'
or message = 'log out'
)
order by created asc
我最大的问题是如何在 CodeIgniter 的 get_where 语句中链接“and”和“or”?我已经查看并看到我可以将东西放入数组中的“WHERE”部分,但我还没有看到如何将东西放入“或”部分(因为数组中的所有内容都是“和”。我必须使用get_where(不能执行“get->where”),所以如果有办法做到这一点,请告诉我!
感谢您花时间阅读我的问题!
自 Codeigniter 3.0 起,查询构建器类支持查询分组
来自文档:
$this->db->select('*')->from('my_table')
->group_start()
->where('a', 'a')
->or_group_start()
->where('b', 'b')
->where('c', 'c')
->group_end()
->group_end()
->where('d', 'd')
->get();
// Generates:
// SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'
编辑: 在您的示例中,您将使用:
$this->db->select('message, created')->from('logs')
->where('username', 'user')
->group_start()
->where('created >', '1487695796')
->where('created <', '1487782196')
->group_end()
->group_start()
->where('message', 'login failure')
->or_where('message', 'login success')
->or_where('message', 'log out')
->group_end()
->order_by('created', 'ASC')
->get();
或
$this->db->select('message, created')
->group_start()
->where('created >', '1487695796')
->where('created <', '1487782196')
->group_end()
->group_start()
->where('message', 'login failure')
->or_where('message', 'login success')
->or_where('message', 'log out')
->group_end()
->order_by('created', 'ASC')
->get_where('logs', array('username' => 'user') );
// both generate:
//SELECT `message`, `created`
//FROM `logs`
//WHERE `username` = 'user'
//AND ( `created` > '1487695796' AND `created` < '1487782196' )
//AND ( `message` = 'login failure' OR `message` = 'login success' OR `message` = 'log out' )
//ORDER BY `created` ASC
要检查 Codeigniter 生成的查询是否与您的 SQL 匹配,您可以使用:
echo $this->db->last_query(); // echos last query string
请尝试这个
$q=$this->db
->select('message,created')
->where('username','user')
->where('created >','1487695796')
->where('created <','1487782196')
->where('message','login failure')
->or_where('message','login success')
->or_where('message','log out')
->order_by('created, ASC')
->get('logs');
return $q->result();
$this->db->select('message, created');
$this->db->where(['username' => '\'user\'', 'created >' => '1487695796', 'created <' => '1487782196']);
$this->db->where("(message='login failure' OR message='login success' OR message='log out')", NULL, FALSE);
$this->db->order_by('created', 'ASC');
$query = $this->db->get('logs');
return $query->result();
好消息,您的查询不需要任何 OR 条件或括号分组。
return $this->db
->select('message, created')
->where_in(
'message',
['login failure', 'login success', 'log out']
)
->order_by('created', 'ASC')
->get_where(
'logs',
[
'username' => 'user',
'created >' => 1487695796,
'created <' => 1487782196,
]
)
->result();
渲染的 SQL:
SELECT `message`, `created`
FROM `logs`
WHERE `message` IN ('login failure', 'login success', 'log out')
AND `username` = 'user'
AND `created` > 1487695796
AND `created` < 1487782196
ORDER BY `created` ASC