各位开发人员好,我正在尝试将我的查询转换成有说服力的代码,但是当我在其他命令中使用“case”命令时,我决定将select部分插入db::raw,但是现在我遇到了一些语法错误或访问错误violation:1055,这是我的sql:
SELECT pa.id_budget_total,pa.name,CASE WHEN co.budget IS null THEN pa.amount ELSE pa.amount - SUM(co.budget) END AS left
FROM budget_total pa LEFT JOIN convening co ON pa.id_budget_total = co.budget_total
where pa.status = 1 GROUP BY pa.name
我把它转换成了雄辩,但是当我添加co.budget时,它告诉我错误代码后面的co.budget不是groupby,是的,如果我在groupby中添加它,它不会显示错误,但是这会改变整个结果。下面是雄辩的密码:
budget_total::select(DB::raw("pa.id_budget_total,pa.name, CASE WHEN co.budget IS null THEN pa.amount ELSE pa.amount - SUM(co.budget) END AS restante"))
->from("budget_total as pa")->leftJoin("convocatoria as co","pa.id_budget_total","=","co.id_budget_total")->where("pa.status", 1)
->groupBy("pa.id_budget_total","pa.name","pa.amount")->get();
1条答案
按热度按时间56lgkhnf1#
一种选择是始终使用
ELSE
条件,但是COALESCE
一NULL
求和为零。