Laravel控制器SQL:连接两个表使输出相乘
Laravel Controller SQL: joining two tables multiplies the output
快速总结一下,我的第一个 table 的行数乘以第二个 table 的行数,这可能是由于我的加入:
Table 1:(bbr_group) 我只有两行
1
Table 2:(bbr_group_type)我只需要得到group_type_name
,加入group_type_id
2
控制器:
public function fetchgroup(){
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
->orderBy('group_id', 'ASC')->get();
return response()->json([
'all_groups'=>$all_groups,
]);
}
Table:(含ajax)
success: function (response){
var tbody="";
$.each(response.all_groups, function (key, group) {
tbody+=`
<tr>
<td><p class="font-weight-bold mb-0">${group.group_name}</p>${group.group_description}</td>
<td><p>${group.group_type_name}</p></td>
<td><p>users here</p></td>
<td><p>status here</p></td>
<td>
<button type="button" value="${group.group_id}" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button>
<button type="button" value="${group.group_id}" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>
</td>
</tr>`;
});
$('#main-group-list tbody').html(tbody)
}
这是我的输出情况:如您所见,即使只有两行,它也显示了第二行中包含每个组名的行 table
3
我需要用左连接 group_type_name
显示 hms_bbr_group
中只有两行的 table。在这种情况下,两者都是“General”
我不确定控制器中的连接或 table 是否是重复的原因。如果这条线是正确的,我想要一些反馈:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
任何帮助都会很棒。谢谢
您的加入条件有误。您在相等的两边都使用 hms_bbr_group
table。我认为这将如下所示:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->orderBy('group_id', 'ASC')->get();
快速总结一下,我的第一个 table 的行数乘以第二个 table 的行数,这可能是由于我的加入:
Table 1:(bbr_group) 我只有两行
1
Table 2:(bbr_group_type)我只需要得到group_type_name
,加入group_type_id
2
控制器:
public function fetchgroup(){
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
->orderBy('group_id', 'ASC')->get();
return response()->json([
'all_groups'=>$all_groups,
]);
}
Table:(含ajax)
success: function (response){
var tbody="";
$.each(response.all_groups, function (key, group) {
tbody+=`
<tr>
<td><p class="font-weight-bold mb-0">${group.group_name}</p>${group.group_description}</td>
<td><p>${group.group_type_name}</p></td>
<td><p>users here</p></td>
<td><p>status here</p></td>
<td>
<button type="button" value="${group.group_id}" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button>
<button type="button" value="${group.group_id}" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>
</td>
</tr>`;
});
$('#main-group-list tbody').html(tbody)
}
这是我的输出情况:如您所见,即使只有两行,它也显示了第二行中包含每个组名的行 table
3
我需要用左连接 group_type_name
显示 hms_bbr_group
中只有两行的 table。在这种情况下,两者都是“General”
我不确定控制器中的连接或 table 是否是重复的原因。如果这条线是正确的,我想要一些反馈:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
任何帮助都会很棒。谢谢
您的加入条件有误。您在相等的两边都使用 hms_bbr_group
table。我认为这将如下所示:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->orderBy('group_id', 'ASC')->get();