How to apply distinct in payment_user_history.sid

dhjboa 注册会员
2023-01-25 20:36

You can use the ->distinct() modifier to return unique rows. However, payment_user_history.id needs to be removed from your select statement for the query to only return one row per 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')

If you need to keep the payment_user_history.id field, then you have to decide how to aggregate it to one row (e.g., using the max or min and a group by clause).

About the Author

Question Info

Publish Time
2023-01-25 20:36
Update Time
2023-01-25 20:36