gpt4 book ai didi

php - 如何使用 Laravel 进行左外连接?

转载 作者:IT老高 更新时间:2023-10-28 23:44:26 26 4
gpt4 key购买 nike

我想要一张表中的信息,如果还有另一张表中的匹配信息。

这是我的代码

 $scoreObject = DB::table('responses')
->select('responses.id', 'responses.questions_id', 'responses.answer_id', 'responses.open_answer', 'responses.user_id', 'responses.scan_id',
'questions.question', 'questions.question_nr', 'questions.type', 'questions.totalsection_id',
'answers.id as answerID', 'answers.answer', 'answers.questions_id', 'answers.points'
)
->Join('answers as answers', 'responses.answer_id', '=', 'answers.id')
->Join('questions as questions', 'answers.questions_id', '=', 'questions.id')
->orderBy('questions.id', 'ASC')
->where('responses.scan_id', $scanid)
->where('responses.user_id', $userid)
->groupBy('questions.id')
->get();

它返回与答案匹配的所有回复 (answers.questions_id questions.id')。有些回复不匹配(因为没有responses.answer_id),但我仍然想要回复信息。

如何在 laravel 中获得这样的左外连接?

最佳答案

您可以尝试将连接指定为左外连接:

->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')

join方法的第四个参数是$type,不指定时默认为inner的值。但是由于 left joinleft outer jointhe same thing ,您可以只使用 leftJoin 方法,使其更具可读性:

->leftJoin('answers as answers', 'responses.answer_id', '=', 'answers.id')

关于php - 如何使用 Laravel 进行左外连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28481321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com