Dav*_*ans 28 php mysql laravel
我想要一个表中的信息,以及是否还有来自另一个表的匹配信息.
这是我的代码
$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();
Run Code Online (Sandbox Code Playgroud)
它返回所有与答案匹配的答案(answers.questions_id questions.id').一些响应没有匹配(因为没有respond.answer_id)但我仍然想要响应信息.
我怎样才能在laravel中获得这样一个左外连接?
Bog*_*dan 56
您可以尝试将连接指定为左外连接:
->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')
Run Code Online (Sandbox Code Playgroud)
join方法的第四个参数是$type,未指定时,默认为该值inner.但是由于左连接和左外连接是相同的,所以你可以改用leftJoin它来使它更具可读性:
->leftJoin('answers as answers', 'responses.answer_id', '=', 'answers.id')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33645 次 |
| 最近记录: |