【发布时间】:2015-07-13 12:02:04
【问题描述】:
我需要帮助才能从我的原始 SQL 查询构建 Laravel 查询。我尝试了很多方法,但没有找到我的运气。有谁能够帮我?我的原始 SQL 代码如下所示。
SELECT exams. * , count( question_details.exam_id ) AS qus_enter
FROM exams
INNER JOIN question_details ON exams.id = question_details.exam_id GROUP BY exams.id
这是我尝试过的:
$examListsID = DB::table('exams')
->join('question_details', function($join) {
$join->on('exams.id', '=', 'question_details.exam_id as qus_enter');
})
->whereraw('count(qus_enter) = exams.total_question')
->select('exams.id as examID','qus_enter','exams.total_question')
->count('qus_enter')
->groupby('exams.id')
->get();
$examLists = Addexam::where('id','=',$examListsID->examID)
我得到这个错误:
SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“as
qus_enterwhere count(qus_enter) = Exams.total_question”附近使用正确的语法(SQL:select count(qus_enter) as aggregate from @987654325 @inner joinquestion_detailsonexams.id=question_details.exam_idasqus_enterwhere count(qus_enter) = Exams.total_question)
【问题讨论】:
-
那么请告诉我们你实际尝试了什么
-
我有两张表,一张是'exams'和'question_details'。在我的“考试”表中,我设置了该考试的总题目,并在“问题详细信息”中输入了带有“考试 ID”的问题。我只想加载在“question_details”表中插入所有问题的考试。
-
我的意思是我希望你用 Laravel 查询代码更新你的答案(即使它不起作用)
-
$examListsID = DB::table('exams') ->join('question_details', function($join) { $join->on('exams.id', '=', 'question_details.exam_id as qus_enter'); }) ->whereraw('count(qus_enter) = Exams.total_question') ->select('exams.id as ExamID','qus_enter','exams.total_question') -> count('qus_enter') ->groupby('exams.id') ->get(); $examLists = Addexam::where('id','=',$examListsID->examID);
-
下次请编辑您的问题以添加详细信息。我现在已经为你完成了......