【发布时间】:2012-03-27 19:40:33
【问题描述】:
我正在使用 CodeIgniter 2.1。
我有一个表,其中包含名字和姓氏以及其他列
我想得到那些有firstname like '%q%' OR lastname like '%q' AND Status = 1的客户
我正在使用以下 Active Record 集:
$this->db->select("$this->table.*, $this->table.Active as 'Status'");
$like = array(
"$this->table.FirstName" => $where['customer_name'],
"$this->table.LastName" => $where['customer_name']
);
$this->db->or_like($like);
生成的查询是:
SELECT `customers`.*, `customers`.`Active` as 'Status'
WHERE `customers`.`Active` = '1' AND
`customers`.`FirstName` LIKE '%michael%' OR
`customers`.`LastName` LIKE '%michael%'
ORDER BY `customers`.`RegDate` desc LIMIT 10
上面的查询会带来名字或姓氏中包含“michael”且活动 = 1 的客户,它会带来所有包含 michael 的记录,无论他们是否处于活动状态。
我需要在名字或姓氏中具有“micheal”且活动 = 1 的记录,我知道在自定义查询中,如果我将 Like 子句放在小括号“()”之间,它将给我想要的结果。 我希望将上述查询解析为:
SELECT `customers`.*, `customers`.`Active` as 'Status'
WHERE `customers`.`Active` = '1' AND
(`customers`.`FirstName` LIKE '%michael%' OR
`customers`.`LastName` LIKE '%michael%')
ORDER BY `customers`.`RegDate` desc LIMIT 10
【问题讨论】:
-
查询中的表名和
from子句在哪里?你忘了$this->db-get()吗? -
亲爱的,我正在做所有事情,代码只是举例,我想在小括号内放置类似的条件。任何想法 ? $this->db->limit($limit, $offset); $query = $this->db->get($this->table);
标签: sql codeigniter sql-like