如何在 payment_user_history.sid 中应用 distinct
How to apply distinct in payment_user_history.sid
这是我在 postman 中的结果
我只想显示一个 sid,即使有 2 个结果
'''
“结果”:[
{
“席德”:2,
“编号”:2,
"user_id": "S01202100002",
"name": "FRANK AMPLE WALKER",
“LRN”:空,
"grade_level": 1
},
{
“席德”:2,
“编号”:4,
"user_id": "S01202100002",
"name": "FRANK AMPLE WALKER",
“LRN”:空,
"grade_level": 1
},
{
“席德”:3,
“编号”:3,
"user_id": "S01202100003",
"name": "NEIL DEST MORGAN",
“LRN”:空,
"grade_level": 2
}
]
}
这是我的代码
public function billingpaymentaccount(){
$fetch = DB::table('payment_user_history')
->leftJoin('tbl_accounts', 'payment_user_history.sid', '=', 'tbl_accounts.id')
->leftJoin('tbl_enrollment', 'payment_user_history.sid', '=', 'tbl_enrollment.student_id')
->select('payment_user_history.sid','payment_user_history.id', 'tbl_accounts.user_id',
DB::raw('concat(tbl_accounts.first_name, " ", tbl_accounts.middle_name, " ",
tbl_accounts.last_name) as name'), 'tbl_accounts.LRN', 'tbl_enrollment.grade_level')
->where('tbl_accounts.account_types',10)
->get();
return response()->json(['results' => $fetch], 200);
}
您可以对 return 唯一行使用 ->distinct()
修饰符。但是,payment_user_history.id
需要从您的 select 语句中删除,以便查询仅 return 每个 sid
一行。
// note the added distinct
$fetch = DB::table('payment_user_history')->distinct()
->leftJoin('tbl_accounts', 'payment_user_history.sid', '=', 'tbl_accounts.id')
->leftJoin('tbl_enrollment', 'payment_user_history.sid', '=', 'tbl_enrollment.student_id')
// note the removed payment_user_history.id
->select('payment_user_history.sid', 'tbl_accounts.user_id', DB::raw('concat(tbl_accounts.first_name, " ", tbl_accounts.middle_name, " ", tbl_accounts.last_name) as name'), 'tbl_accounts.LRN', 'tbl_enrollment.grade_level')
->where('tbl_accounts.account_types',10)
->get();
如果您需要保留 payment_user_history.id
字段,那么您必须决定如何将其聚合到一行(例如,使用 max 或 min 和 group by 子句)。
这是我在 postman 中的结果
我只想显示一个 sid,即使有 2 个结果 '''
“结果”:[
{
“席德”:2,
“编号”:2,
"user_id": "S01202100002",
"name": "FRANK AMPLE WALKER",
“LRN”:空,
"grade_level": 1
},
{
“席德”:2,
“编号”:4,
"user_id": "S01202100002",
"name": "FRANK AMPLE WALKER",
“LRN”:空,
"grade_level": 1
},
{
“席德”:3,
“编号”:3,
"user_id": "S01202100003",
"name": "NEIL DEST MORGAN",
“LRN”:空,
"grade_level": 2
}
]
}
这是我的代码
public function billingpaymentaccount(){
$fetch = DB::table('payment_user_history')
->leftJoin('tbl_accounts', 'payment_user_history.sid', '=', 'tbl_accounts.id')
->leftJoin('tbl_enrollment', 'payment_user_history.sid', '=', 'tbl_enrollment.student_id')
->select('payment_user_history.sid','payment_user_history.id', 'tbl_accounts.user_id',
DB::raw('concat(tbl_accounts.first_name, " ", tbl_accounts.middle_name, " ",
tbl_accounts.last_name) as name'), 'tbl_accounts.LRN', 'tbl_enrollment.grade_level')
->where('tbl_accounts.account_types',10)
->get();
return response()->json(['results' => $fetch], 200);
}
您可以对 return 唯一行使用 ->distinct()
修饰符。但是,payment_user_history.id
需要从您的 select 语句中删除,以便查询仅 return 每个 sid
一行。
// note the added distinct
$fetch = DB::table('payment_user_history')->distinct()
->leftJoin('tbl_accounts', 'payment_user_history.sid', '=', 'tbl_accounts.id')
->leftJoin('tbl_enrollment', 'payment_user_history.sid', '=', 'tbl_enrollment.student_id')
// note the removed payment_user_history.id
->select('payment_user_history.sid', 'tbl_accounts.user_id', DB::raw('concat(tbl_accounts.first_name, " ", tbl_accounts.middle_name, " ", tbl_accounts.last_name) as name'), 'tbl_accounts.LRN', 'tbl_enrollment.grade_level')
->where('tbl_accounts.account_types',10)
->get();
如果您需要保留 payment_user_history.id
字段,那么您必须决定如何将其聚合到一行(例如,使用 max 或 min 和 group by 子句)。