【发布时间】:2021-05-12 14:04:09
【问题描述】:
首先,我不擅长英语,而且我刚开始使用 laravel 框架。我为论文目的开发了一些学校网站。而且我认为这可能是我学习 laravel 的一个机会,因为许多开发人员都推荐 laravel。
回到我的问题嘿嘿
这个查询运行正确。
$questions = DB::table('questions')
->select('questions.id','questions.role','questions.postBody', 'questions.classCode', 'questions.postedById', 'questions.created_at', 'classroom.classroomName','classroom.icon','users.picture', 'students.firstName', 'students.middleName', 'students.lastName', 'students.suffix','students.studentNumber')
->join('classroom', 'classroom.classCode', '=', 'questions.classCode')
->join('users', 'users.id', '=', 'questions.postedById')
->join('students', 'students.email' , '=', 'users.email')
->orderBy('questions.created_at', 'DESC')
->skip($request->page - 5)
->take($request->page)
->get();
当我添加其他表(“老师”)时,我没有得到任何回报。这是我的查询尝试加入“教师”表的示例,在这里,我试图选择教师的电子邮件
$questions = DB::table('questions')
->select('questions.id','questions.role','questions.postBody', 'questions.classCode', 'questions.postedById', 'questions.created_at', 'classroom.classroomName','classroom.icon','users.picture', 'students.firstName', 'students.middleName', 'students.lastName', 'students.suffix','students.studentNumber','teacher.email')
->join('classroom', 'classroom.classCode', '=', 'questions.classCode')
->join('users', 'users.id', '=', 'questions.postedById')
->join('students', 'students.email' , '=', 'users.email')
->join('teacher', 'teacher.email' , '=', 'users.email')
->orderBy('questions.created_at', 'DESC')
->skip($request->page - 5)
->take($request->page)
->get();
抱歉我的英语不好。
谁能帮帮我?非常感谢!
【问题讨论】:
-
您确定表的名称是
teacher而不是teachers? -
@Aless55,是的,我确定。
-
是否有可能没有内连接的结果?您确定有一个用户电子邮件也是教师电子邮件吗?
-
您确定要使用内连接而不是左连接吗?
-
@Aless55,如果我要使用左连接。我如何从中选择字段。和join的方法一样吗?你能给我举个例子吗?