使用 CodeIgniter 活动记录查询对 WHERE 子句逻辑进行分组
我在 sql 查询中使用几个“like”时遇到了问题(使用 Codeigniter 的 activerecord 生成):
SELECT *
FROM (`posts`)
WHERE
`city` LIKE '%test%'
AND `title` LIKE '%%'
OR `text` LIKE '%%'
事情是,它似乎读取此查询,就好像第一个 like 和第二个 like 周围有括号一样,但我希望在第二个和最后一个周围有一个括号(我希望它比较最后一个或倒数第二个是否有效)。
如何使用 Codeigniter 的 Active Record 类实现此目的?
当前代码:
if($type != 0)
$this->db->where('type', $type);
$this->db->like('city', $area);
$this->db->like('title', $words);
$this->db->or_like('text', $words);
return $this->db->get('posts')->result_array();
I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord):
SELECT *
FROM (`posts`)
WHERE
`city` LIKE '%test%'
AND `title` LIKE '%%'
OR `text` LIKE '%%'
Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there to be a parenthesis around the second and the last like (I want it to compare if the last or the next to last works).
How can I achieve this using Codeigniter's Active Record class?
Current code:
if($type != 0)
$this->db->where('type', $type);
$this->db->like('city', $area);
$this->db->like('title', $words);
$this->db->or_like('text', $words);
return $this->db->get('posts')->result_array();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
musoNic80的答案是正确的。我想补充以下内容。
在Codeigniter中总体支持以下内容。
希望这会对某人有所帮助。
musoNic80's answer is correct. I would like to add the following.
In Codeigniter supporting the following overall.
Hope this will help someone.
我认为 CI 没有添加任何括号。它将在前两个语句之间添加“AND”,并在后两个语句之间添加“OR”。为了实现你想要的,我会写我自己的声明。这非常简单。
请注意我是如何使用绑定的。这会自动为您转义字符。
I don't think CI is adding any paranthesis. It will add an 'AND' between the first two statements and an 'OR' between the last two. To achieve what you want, I would write my own statement. It's very straightforward.
Notice how I've used binding. This automatically escapes characters for you.
这就能解决问题
this will do the trick
要封装条件逻辑,请在条件构建方法调用周围使用
group_start()
和group_end()
方法。要构建使用
AND
连接到子句其余部分的封装条件集,请使用group_start()
,对于OR
使用or_group_start( )
。此外,
like()
、not_like()
、or_like()
和or_not_like()
都具有此功能接收关联数组作为第一个参数 - 这些包装器迭代调用受保护的方法_like()
并使用OR
连接条件(如果包装器名称进行了描述),否则AND
。根据数据库方言/驱动程序,引用可能有所不同,但呈现的查询将类似于以下内容:
To encapsulate conditional logic, use
group_start()
andgroup_end()
methods around condition building method calls.To build an encapsulated set of conditions joined to the rest of the clause with
AND
usegroup_start()
, forOR
useor_group_start()
.Also,
like()
,not_like()
,or_like()
, andor_not_like()
all have the ability to receive an associative array as the first parameter -- these wrappers make iterated calls of the protected method_like()
and join the conditions withOR
if described by the wrapper name, otherwiseAND
.Depending on database dialect/driver, quoting may differ, but the rendered query will resemble this:
当您必须使用表连接时,请尝试以下方法
when you have to use table join as well try following method