【问题标题】:Multiple Like Columns in Query查询中的多个类似列
【发布时间】: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


【解决方案1】:

我认为您还需要使用or_where 函数

【讨论】:

  • 我也这样做了,但是我想要的没有完成,我想我必须去 $this->db->query() 因为我的时间不多了......谢谢为您提供帮助
【解决方案2】:

将 where 和 or_like 与多个列放在 codeigniter 中时,分组不正确。 我也遇到过这个问题,

最好的办法是把查询写成字符串格式,然后执行。

【讨论】:

    【解决方案3】:
     //You can used like that 
    
    $this->db->where("(`car_model` LIKE '%$search_keyword_new%' OR  `car_make_model` LIKE '%$search_keyword_new%' OR `car_name` LIKE '%$search_keyword_new%')");
    $this->db->where('user_id',$user_id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-28
      • 2011-01-15
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多