将我的sql查询转换为活动记录查询codeigniter

问题描述 投票:0回答:2
SELECT * FROM cdr WHERE (Circle LIKE '%D%' OR CLI LIKE '%D%' OR Operator LIKE '%D%') AND Dept = 'Sale'

我只想像 $this->db->like('Circle','%D%') 一样,部门带有 'and'。

codeigniter
2个回答
1
投票

您应该阅读 codeigniter 文档,这是一个简单的文档:

$this->db->where("(Circle LIKE '%D%' OR CLI LIKE '%D%' OR Operator LIKE '%D%')")
         ->where("dept", "Sale");
$query = $this->db->get("cdr"); 

http://www.codeigniter.com/user_guide/database/active_record.html


0
投票

只需将关联数组提供给

or_like()
并用
group_start()
group_end()
将这些条件封装在括号中。

return $this->db
    ->group_start()
        ->or_like(['Circle' => 'D', 'CLI' => 'D', 'Operator' => 'D'])
    ->group_end()
    ->get_where('cdr', ['Dept' => 'Sale'])
    ->result();

如果需要,

or_like()
可以更干燥:
->or_like(array_fill_keys(['Circle', 'CLI', 'Operator'], 'D'))

渲染的 SQL:

SELECT *
FROM `cdr`
WHERE (
`Circle` LIKE '%D%' ESCAPE '!'
OR  `CLI` LIKE '%D%' ESCAPE '!'
OR  `Operator` LIKE '%D%' ESCAPE '!'
)
AND `Dept` = 'Sale'
© www.soinside.com 2019 - 2024. All rights reserved.