我是laravel 5 sql查询构建的新手,我面临将sql转换为laravel 5.x sql查询的挑战。
以下是正常的sql:
select users.name as name, count(ticket.id) as total, sum(ticket.quantity * ticket.amount) as montant
from users
inner join program on program.driver_id = users.id
inner join line on line.id = program.line_id
inner join trip on trip.program_id = program.id
inner join ticket on ticket.trip_id = trip.id
where program.date = $date
group by users.name
给了我需要的回复。 但我的代码的laravel版本什么也没有返回。以下是我的代码
$result = \DB::table('users')
->select('users.name as name', \DB::raw('count(ticket.id) as total'),\DB::raw('SUM(ticket.quantity*ticket.amount) as montant'))
->Join('program', 'program.driver_id', '=', 'users.id')
->Join('line', 'line.id', '=', 'program.line_id')
->Join('trip', 'trip.program_id', '=', 'program.id')
->Join('ticket', 'trip.id', '=', 'ticket.trip_id')
->where('program.date', $date)
->groupBy('users.name')
->get();
提前致谢
答案 0 :(得分:0)
您可以尝试以下查询:
use Illuminate\Support\Facades\DB;
$result = DB::table('users')
->selectRaw('users.name as name, count(ticket.id) as total, SUM(ticket.quantity*ticket.amount) as montant'))
->join('program', 'program.driver_id', '=', 'users.id')
->join('line', 'line.id', '=', 'program.line_id')
->join('trip', 'trip.program_id', '=', 'program.id')
->join('ticket', 'ticket.trip_id', '=', 'trip.id')
->where('program.date', $date)
->groupBy('users.name','ASC')
->get();