【发布时间】:2017-09-11 14:58:29
【问题描述】:
我是 laravel 的新手,我创建了一个在 Xampp 服务器中完美运行的查询,现在我想在 laravel 中转换这个查询,我创建了一个显示错误 SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'question_chapter_rel' (SQL: select count(*) as aggregate fromquestion_chapter_relinner joinquestion_chapter_rel@ 的 laravel 代码987654331个@章. ID @ 987654333个@ question_chapter_rel . chapter_id inner join问题on问题. ID = question_chapter_rel . question_id inner join答案on答案. ID = questions.correct_answerswhereanswers.is_correct= 1)
这是我的 laravel 查询:
public function scopeGetQuestion($query)
{
return $query->join('question_chapter_rel', 'chapters.id', '=' ,'question_chapter_rel.chapter_id')
->join('questions','questions.id', '=' ,'question_chapter_rel.question_id')
->join('answers','answers.id', '=' ,'questions.correct_answers')
->select(
[
'chapters.id',
'chapters.chapter_description',
'questions.id',
'questions.question_description',
'answers.id',
'answers.answer_description'
]
)
->where('answers.is_correct',1)
->paginate(12);
}
这是我要转换的代码
select `chapters`.`id`, `chapters`.`chapter_description`, `questions`.`id`, `questions`.`question_description`, `answers`.`id`, `answers`.`answer_description`
from `chapters`
Inner Join `question_chapter_rel`
on `chapters`.`id` = `question_chapter_rel`.`chapter_id`
Inner Join `questions`
on `questions`.`id` = `question_chapter_rel`.`question_id`
Inner Join `answers`
on `answers`.`id` = `questions`.`correct_answers`
where `answers`.`is_correct` = 1
提前致谢
【问题讨论】:
标签: mysql sql database laravel-5.2