codeigniter中的活动记录会自动在每个where子句的逗号分隔值周围添加引号

时间:2015-05-19 11:38:38

标签: mysql codeigniter select activerecord

我传递的逗号分隔值在WHERE IN Query子句中用作

$sql = '
            MAX(CASE WHEN rt.machine_name = "new" THEN rt.request_status_count ELSE "0" END) AS "new",
            MAX(CASE WHEN rt.machine_name = "open" THEN rt.request_status_count ELSE "0" END) AS "open",
            MAX(CASE WHEN rt.machine_name = "waiting" THEN rt.request_status_count ELSE "0" END) AS "waiting",
            MAX(CASE WHEN rt.machine_name = "closed" THEN rt.request_status_count ELSE "0" END) AS "closed"
            FROM 
            (SELECT COUNT(r.id) AS request_status_count, rs.name AS status, rs.machine_name AS machine_name
            FROM '. $this->_table . ' r INNER JOIN request_status AS rs ON r.request_status_id = rs.id INNER JOIN organizations AS o ON r.organization_id = o.id '. $where_string .' GROUP BY r.request_status_id) AS rt';

        $this->db->select($sql);  

$ where_string以

的形式传递
$security_clause_string = 'WHERE r.location_id IN ("' . $this->session->userdata('str_user_locations').'")';

str_user_locations = 1,2,4

在哪里

此查询的输出是

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` GROUP BY r.request_status_id) AS rt' at line 4

SELECT MAX(CASE WHEN rt.machine_name = "new" THEN rt.request_status_count ELSE "0" END) AS "new", MAX(CASE WHEN rt.machine_name = "open" THEN rt.request_status_count ELSE "0" END) AS "open", MAX(CASE WHEN rt.machine_name = "waiting" THEN rt.request_status_count ELSE "0" END) AS "waiting", MAX(CASE WHEN rt.machine_name = "closed" THEN rt.request_status_count ELSE "0" END) AS "closed" FROM (SELECT COUNT(r.id) AS request_status_count, `rs`.`name` AS status, `rs`.`machine_name` AS machine_name FROM requests r INNER JOIN request_status AS rs ON r.request_status_id = rs.id INNER JOIN organizations AS o ON r.organization_id = o.id WHERE r.location_id IN ("1, `2`, `3")` GROUP BY r.request_status_id) AS rt

2 个答案:

答案 0 :(得分:0)

尝试where_in阅读where_in

$this->db->where_in('r.location_id', $this->session->userdata('str_user_locations'));

where_in产生这样的结果

 $names = array('Frank', 'Todd', 'James');
    $this->db->where_in('username', $names);
 // Produces: WHERE username IN ('Frank', 'Todd', 'James')

答案 1 :(得分:0)

Codeignitor Active记录添加了这些引号,我用

替换了我的查询
$sql = 'SELECT
            MAX(CASE WHEN rt.machine_name = "new" THEN rt.request_status_count ELSE "0" END) AS "new",
            MAX(CASE WHEN rt.machine_name = "open" THEN rt.request_status_count ELSE "0" END) AS "open",
            MAX(CASE WHEN rt.machine_name = "waiting" THEN rt.request_status_count ELSE "0" END) AS "waiting",
            MAX(CASE WHEN rt.machine_name = "closed" THEN rt.request_status_count ELSE "0" END) AS "closed"
            FROM 
            (SELECT COUNT(r.id) AS request_status_count, rs.name AS status, rs.machine_name AS machine_name
            FROM '. $this->_table . ' r INNER JOIN request_status AS rs ON r.request_status_id = rs.id INNER JOIN organizations AS o ON r.organization_id = o.id '. $where_string .' GROUP BY r.request_status_id) AS rt';

        $query = $this->db->query($sql);  
        return $query->result_array();