在我的Laravel 5.8项目中,我有以下查询:
$approval_record = HrEmployee::selectRaw('count(is_approved) as count,is_approved, if (is_approved = 1, "Awaiting", "Approved") as approval')->where('company_id', $userCompany)->where('hr_status', 0)->groupBy('is_approved')->get();
在我的数据库中,已批准有1、2和任何其他数字。
1正在等待,2已批准,然后还有其他号码吗?
如何在上面的代码中修改IF语句以实现此目的?
答案 0 :(得分:0)
感谢此article
您可以使用案例:
$approval_record = HrEmployee::selectRaw('count(is_approved) as count,is_approved,
(CASE COALESCE (is_approved, 0) WHEN "1" THEN "Awaiting" WHEN "2" THEN "Approved"
WHEN "0" THEN "empty" ELSE "somethingElse"
)as approval')
->where('company_id', $userCompany)->where('hr_status', 0)->groupBy('is_approved')->get();