MySQL限制和偏移错误

时间:2016-03-04 10:48:50

标签: sql select pagination limit codeigniter-3

我正在进行此分页,我的查询出现此错误

Error Number: 1064

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 'LIMIT 0 , 10' at line 9

SELECT job_availability.JOB_CODE, job_titles.JOB_NAME, job_availability.AVAILABILITY FROM job_titles LEFT JOIN job_availability ON job_availability.JOB_CODE = job_titles.JOB_CODE ORDER BY job_titles.JOB_NAME, LIMIT 0 , 10

我正在尝试这样做

function get_department_list($limit, $start)
{
    $sql = 'select var_dept_name, var_emp_name from tbl_dept, tbl_emp where tbl_dept.int_hod = tbl_emp.int_id order by var_dept_name limit ' . $start . ', ' . $limit;
    $query = $this->db->query($sql);
    return $query->result();
}

我的代码是

public function get_list($limit , $start){

    $sql = ('SELECT 
                job_availability.JOB_CODE, 
                job_titles.JOB_NAME, 
                job_availability.AVAILABILITY
            FROM job_titles
            LEFT JOIN job_availability
            ON job_availability.JOB_CODE = job_titles.JOB_CODE
            ORDER BY job_titles.JOB_NAME,
            LIMIT ? , ?');
    $data = array($start, $limit);
    $query =  $this->db->query($sql, $data);
}

我不认为我的代码有问题,所以我不确定错误是什么。我的MySQL版本是5.6.24。

1 个答案:

答案 0 :(得分:3)

,子句之前的order by子句末尾有一个冗余逗号(limit)。只需删除它,你就可以了:

SELECT 
    job_availability.JOB_CODE, 
    job_titles.JOB_NAME, 
    job_availability.AVAILABILITY
FROM job_titles
LEFT JOIN job_availability
ON job_availability.JOB_CODE = job_titles.JOB_CODE
ORDER BY job_titles.JOB_NAME
-- Comma removed here ------^
LIMIT ? , ?