gpt4 book ai didi

sql - 查询中的多个类似列

转载 作者:行者123 更新时间:2023-11-30 23:49:08 24 4
gpt4 key购买 nike

我正在使用 CodeIgniter 2.1。

我有一个表,其中包含名字和姓氏以及其他列我想获得那些 firstname like '%q%' OR lastname like '%q' AND St​​atus = 1

的客户

我正在使用以下事件记录集:

$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”且 active = 1 的那些客户,它带来了所有有 michael 的记录,无论他们是否活跃。

我需要那些在名字或姓氏中包含“micheal”且 active = 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

最佳答案

我认为您还需要利用 or_where功能

关于sql - 查询中的多个类似列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9896512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com