Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como escrever a consulta Laravel orderBy?


sua pergunta não está clara para orderBy . Com base na sua pergunta:
SELECT
    report_doctor_company_daily_test.test_id,
    testing_types.testing_name,
    doctor.first_name,
    doctor.last_name,
    SUM( report_doctor_company_daily_test.test_price )  FROM
    testing_types
    INNER JOIN report_doctor_company_daily_test ON testing_types.id = report_doctor_company_daily_test.test_id
    INNER JOIN doctor ON report_doctor_company_daily_test.doctor_id = doctor.id  GROUP BY
    testing_t
ypes.id

Resposta:

$test = ReportDoctorCompanyDailyTestModal::select(
        'report_doctor_company_daily_test.test_id',
        'testing_types.testing_name',
        'doctor.first_name',
        'doctor.last_name',
        DB::raw('SUM(report_doctor_company_daily_test.test_price)')
    )
    ->join('report_doctor_company_daily_test', 'testing_types.id', '=', 'report_doctor_company_daily_test.test_id')
    ->join('doctor', 'report_doctor_company_daily_test.doctor_id', '=', 'doctor.id')
    ->groupBy('testing_types.id');

dd($test->toSql());

Resultado:

select 
     `report_doctor_company_daily_test`.`test_id`, 
     `testing_types`.`testing_name`, 
     `doctor`.`first_name`, 
     `doctor`.`last_name`, 
     SUM(report_doctor_company_daily_test.test_price) 

from 
    `testing_types` 

inner join `report_doctor_company_daily_test` 
    on `testing_types`.`id` = `report_doctor_company_daily_test`.`test_id` 

inner join `doctor` 
    on `report_doctor_company_daily_test`.`doctor_id` = `doctor`.`id` 

group by `testing_types`.`id`