【发布时间】:2021-01-10 03:03:24
【问题描述】:
我想从 4 个表中提取数据。
如 sname、cgpa、Group_Cgpa、Room_No 和旅馆名称。
表关系如下。
AllotmentApps 有:id、sname、cgpa 和 grpID(来自 Group id 表)列。
群组有:id,Group_CGPA
房间有:id、HostelID(来自 Hostel)和 ResidentID(来自 Group id)。
Hostels 有:id、Name、Total_Rooms。
我在加入表格时遇到了问题。加入 AllotmentApps 和 Groups 工作正常,但是当我加入 Room and Hostel Table 时出现问题。
AllotmentApps 和群组的加入
public function FinalList()
{
$allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.grpID')->orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
function($item) {
return $item->grpID;
}
) ->take(3);
4 个表的连接
public function FinalList()
{
$allot_apps = AllotmentApps::select('sname','cgpa','Group_CGPA','Hostel_Name')->join('Groups','Groups.id','AllotmentApps.grpID')->join('Hostels','Hostels.id','Hostels.HostelID')->join('Rooms','Rooms.id','Hostel.HostelID')->orderBy('Groups.id', 'Asc')->get()->groupBy(
function($item) {
return $item->grpID;
}
) ->take(3);
【问题讨论】: