【发布时间】:2021-05-28 10:29:28
【问题描述】:
如何在 Laravel 中编写 SQL 查询
SELECT
notifications.*,
if(notifications.branchID=0, 'All', (
select
group_concat(name)
from
branches
where
find_in_set(id,notifications.branchID)
)) as brcName
FROM
notifications
WHERE
id = 2
【问题讨论】:
-
首先在 SQL RAW 查询中正确编写它,这样我们就可以将其“翻译”为 elloquent Buery Builder 或类似的东西......并格式化您的代码。
-
@hicham-o-sfh 这个东西会在 mysql 上返回完美的结果
-
所以请提及您的 MySQL 版本,并发布您所有格式正确的代码。
-
得到了解决方案老兄,干杯! ✔????解决方案是 DB::select( DB::raw("SELECT notifications.*, if(notifications.branchID=0,'All',(select group_concat(name) FROM branches where find_in_set(id,notifications.branchID))) as brcName FROM 通知 WHERE id = :id"), array( 'id' => $id, ));
标签: php mysql laravel eloquent