【发布时间】:2011-09-27 00:36:35
【问题描述】:
我正在使用 ActiveRecord 生成如下查询
SELECT * FROM (`foods`) WHERE `type` = 'fruits' AND
`tags` LIKE '%green%' OR `tags` LIKE '%blue%' OR `tags` LIKE '%red%'
tags 的数量和值未知。数组是动态创建的。下面我添加了一个可能的数组。
$tags = array (
'0' => 'green'.
'1' => 'blue',
'2' => 'red'
);
有一个标签数组,我使用以下循环来创建我在顶部发布的查询。
$this->db->where('type', $type); //var type is retrieved from input value
foreach($tags as $tag):
$this->db->or_like('tags', $tag);
endforeach;
问题:我需要在LIKE 子句周围添加括号,如下所示:
SELECT * FROM (`foods`) WHERE `type` = 'fruits' AND
(`tags` LIKE '%green%' OR `tags` LIKE '%blue%' OR `tags` LIKE '%red%')
如果括号内的内容是静态的,但 foreach 循环让我失望,我知道如何完成此操作..
【问题讨论】:
标签: mysql arrays codeigniter activerecord