2016-09-27 43 views
-2

我得到下面的mysql數據庫錯誤錯誤號:1064您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法

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 'option, GROUP_CONCAT(DISTINCT year_question_map.year) AS years, GROUP_CONCAT(DIS' at line 1 

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 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

我使用笨,這裏的手冊是我的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; 
    } 
+0

'question_option.question_option' ??或'question_option.OPTIONNAME'? – devpro

+0

question_option.question_option NOT question_option.OPTIONNAME – imZiaur

+0

可能因爲同一個表格和相同的列名稱而出現錯誤。 – devpro

回答

1

您正在使用MYSQL Reserve單詞查詢:

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)與任何字下面參考給定表示這是一個保留字。

+0

感謝@devpro指出了錯誤和現在的參考它像魅力一樣工作。再次感謝 – imZiaur

相關問題