Codeigniter的活动记录有几个喜欢还是? [英] Codeigniter active record with several like or?
问题描述
嘿..我在使用sql查询(使用Codeigniter的activerecord生成)中使用几个赞"时遇到了问题:
Hey.. 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 '%%'
事实是,似乎在读取此查询时好像第一个like和第二个like周围有一个括号,但我希望第二个和最后一个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).
如何使用Codeigniter的Active Record类实现此目标?
How can I achieve this using Codeigniter's Active Record class?
当前代码: if($ type!= 0) $ this-> db-> where('type',$ type);
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();
推荐答案
我不认为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.
$sql = SELECT * FROM 'posts' WHERE city LIKE ? AND ( title LIKE ? OR text LIKE ? );
$query = $this->db->query($sql, array($area, $words, $words));
注意我如何使用绑定.这会自动为您转义字符.
Notice how I've used binding. This automatically escapes characters for you.
这篇关于Codeigniter的活动记录有几个喜欢还是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!