连接查询中的mysql语法错误

时间:2014-10-26 05:24:45

标签: php mysql

我遇到以下查询的mysql语法错误。

SELECT student_info.grade,
       student_info.section,
       student_info.student_name,
       mwf.*
FROM student_info
INNER JOIN mwf ON student_info.student_gen_id = mwf.mwf_student_id
WHERE mwf.mwf_month =".$current_month

错误

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在'附近使用。 2014'在第4行

此处mwf_month是字符串类型,我传递的值为current_month october, 2014

我做错了什么?提前谢谢。

更新

生成查询的完整模型函数

public function transaction_report_total($current_month) {
        $sql = "SELECT student_info.grade,
                student_info.section,
                student_info.student_name,
                mwf.*
                FROM student_info
                INNER JOIN mwf ON student_info.student_gen_id = mwf.mwf_student_id
                WHERE mwf.mwf_month = '" . $current_month."'";

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

1 个答案:

答案 0 :(得分:1)

由于mwf_month的类型是字符串,因此您需要在$current_month之间添加单引号,如下所示

SELECT student_info.grade, student_info.section, student_info.student_name, mwf.* 
FROM student_info
INNER JOIN mwf ON student_info.student_gen_id = mwf.mwf_student_id
WHERE mwf.mwf_month = '".$current_month."'

<强>更新

看起来SQL语句在$current_month之后结束,所以实际上需要最后的双引号

$sql = "SELECT student_info.grade,
        student_info.section,
        student_info.student_name,
        mwf.*
        FROM student_info
        INNER JOIN mwf ON student_info.student_gen_id = mwf.mwf_student_id
        WHERE mwf.mwf_month = '".$current_month."'";