问题描述
我想要一个表中的信息,如果还想要另一个表中的信息,则同样.
i want information from one table and if there is matched info from another table that as well.
这是我的代码
$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_idquestions.id').一些响应不匹配(因为没有response.answer_id),但是我仍然想要响应信息.
It returns all responses that have matches with answers (answers.questions_id questions.id'). some responses don't have matched (because there is no responses.answer_id) but i still want the responses info then.
我如何在laravel中获得这种左外部连接?
how can i get such a left outer join in laravel ?
推荐答案
您可以尝试将连接指定为左外部连接:
You could try specifying the join as being a left outer join:
->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')
join方法的第四个参数是$type
,如果未指定,则默认为值inner
.但是由于左连接和左外部连接是同一件事 ,您可以改为使用leftJoin
方法,以使其更具可读性:
The fourth parameter of the join method is $type
, which when not specified, defaults to the value inner
. But since left join and left outer join are the same thing, you could just use the leftJoin
method instead, to make it more readable:
->leftJoin('answers as answers', 'responses.answer_id', '=', 'answers.id')
这篇关于如何与Laravel一起加入Left Outer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!