将查询从SQL转换为Laravel格式

时间:2019-04-08 20:09:42

标签: mysql sql laravel

我需要将此查询转换为查询。我已经阅读了文档,但是对于我来说仍然太混乱且难以完成任务。我在Google上进行了搜索,但找不到任何解决方案。

SELECT u.name , 
           ((SUM(tot_expenseamount)/ SUM(tot_noofmeal))*tot_noofmeal) as total_Cost,
           IFNULL(t.tot_expenseamount,0) AS tot_expense_amount ,
           IFNULL(n.tot_noofmeal,0) AS tot_no_of_meal 
    FROM users u
    LEFT JOIN (
        SELECT e.user_id , SUM(e.expenseamount) AS tot_expenseamount 
        FROM expenses e 
        WHERE e.expensedate >= '2019-04-01' + INTERVAL 0 MONTH AND e.expensedate < '2019-04-01' + INTERVAL 1 MONTH
        GROUP BY e.user_id ) t ON t.user_id = u.id
    LEFT JOIN (
        SELECT m.user_id , SUM(m.noofmeal) AS tot_noofmeal 
        FROM meals m 
        WHERE m.mealdate >= '2019-04-01' + INTERVAL 0 MONTH AND 
       m.mealdate < '2019-04-01' + INTERVAL 1 MONTH
        GROUP BY m.user_id ) n ON n.user_id = u.id 
     GROUP BY u.name 
     ORDER BY u.name

0 个答案:

没有答案