我有以下3个表:表,保留和table_reservation。最后一个是枢轴,问题是我想根据给定的日期对每个保留的金额求和,但不能重复,也就是说,有一个保留了3个表的保留,这3个记录被添加到枢轴中表,总和是我不想要的保留金额的3倍。
我离开桌子和代码。
table_reservation
| date | table_id | reservation_id |
| 2019-06-12 | 2 | 1 |
| 2019-06-12 | 3 | 1 |
| 2019-06-12 | 4 | 1 |
| 2019-06-12 | 1 | 2 |
reservation
| id | name | emp_id | amount | other |
| 1 | Luis Miguel | 2 | 200 | vdvvdvdv |
| 2 | carloz | 2 | 234 | dwdq |
tables
| id | name |
| 1 | A1 |
| 2 | B2 |
| 3 | A2 |
| 4 | A4 |
| 6 | B4 |
PHP代码
DB::table('reservation')
->join('table_reservation', 'reservation.id', '=', 'table_reservation.reservation_id')
->join('tables', 'table_reservation.mesa_id', '=', 'tables.id')
->join('empleados', 'reservation.emp_id', '=', 'emp.id')
->select('reservation.id',DB::raw('group_concat(tables.name) as y'),'reservation.name', 'emp.name as x','reservation.amount','reservation.other', 'table_reservation.date', DB::raw("SUM(reservation.amount) as count"))
->select(DB::raw("SUM(reservation.amount) as count"))
->distinct()
->groupBy('reservation.id')
->where('table_reservation.date',$date)->get();
我得到以下信息:
[{"count":"600"},{"count":"234"}]
应该是200,而不是600