【问题标题】: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错误号:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法
【发布时间】:2016-09-27 07:50:29
【问题描述】:

我遇到mysql数据库错误

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'option, GROUP_CONCAT(DISTINCT year_question_map.year) AS years, GROUP_CONCAT(DIS' 附近使用正确的语法

SELECT `questions`.*, `question_level`.*, `question_answer`.*, GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS 选项, GROUP_CONCAT(DISTINCT year_question_map.year) AS 年, GROUP_CONCAT(DISTINCT Exams.exam_name) AS 考试 FROM `questions` LEFT JOIN `question_option` ON `question_option`.`question_id` = `questions`.`qid` LEFT JOIN `question_level` ON `question_level`.`level_id` = `questions`.`level_id` LEFT JOIN `question_answer` ON `question_answer`.`question_id` = `questions`.`qid` LEFT JOIN `year_question_map` ON `year_question_map`.`question_id` = `questions`.`qid` LEFT JOIN `exams` ON `exams`。 `exam_id` = `year_question_map`.`exam_id` WHERE `questions`.`topic_id` = '1' GROUP BY `questions`.`qid` ORDER BY `qid` ASC 

我正在使用 Codeigniter,这是我模型中的 sql 查询

        $this->db->select("questions.*,question_level.*,question_answer.*,GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS option,GROUP_CONCAT(DISTINCT year_question_map.year) AS years,GROUP_CONCAT(DISTINCT exams.exam_name) AS exams");
    $this->db->from('questions');
    $this->db->join('question_option','question_option.question_id = questions.qid','left');
    $this->db->join('question_level','question_level.level_id = questions.level_id','left');
    $this->db->join('question_answer','question_answer.question_id = questions.qid','left');
    $this->db->join('year_question_map','year_question_map.question_id = questions.qid','left');
    $this->db->join('exams','exams.exam_id = year_question_map.exam_id','left');    
    $this->db->where('questions.topic_id',$topicID);
    $this->db->group_by('questions.qid');
    $this->db->order_by('qid','ASC');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
    return $query->result();
} else {
      return FALSE;
  }

【问题讨论】:

  • question_option.question_option ??或question_option.OPTIONNAME??
  • question_option.question_option 不是 question_option.OPTIONNAME
  • 可能因为同一个表和同一个列名而出错..
  • 我已经使用不同的名称进行了检查,但它给出了相同的错误

标签: php mysql database codeigniter join


【解决方案1】:

您在查询中使用 MYSQL Reserve Word:

GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS OPTION,

请注意,OPTION 是一个保留字,您必须使用反引号或将其更改为其他名称。

这应该是:

GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS `OPTION`,

作为参考,您可以在此处查看保留词列表: https://dev.mysql.com/doc/refman/5.5/en/keywords.html

(R) 与下面给出的任何单词参考表示这是一个保留单词。

【讨论】:

  • 感谢@devpro 指出错误并提供参考,现在它就像魅力一样工作。再次感谢
猜你喜欢
  • 2017-06-01
  • 2015-10-23
  • 2019-10-24
  • 2018-04-04
  • 2017-09-12
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
相关资源
最近更新 更多