php - 如何将 select 中的内部查询转换为 Eloquent laravel 5.4

标签 php mysql laravel laravel-5

我有一个这样的查询..

select *,
case l.user_type 
when '0' then 
(select CONCAT(first_name,'',last_name) from users where id=l.user_id) 
when '1' then 
(select party_name from tbl_partys where id=l.user_id) 
end as user_name 
from tbl_leased_comm l 
where l.user_id=$party and l.user_id=$user_id 
order by l.updated_at desc

如何将此查询转换为 Laravel 查询 任何人都可以帮助我...

最佳答案

是的,它对我有用

作为,

DB::table('tbl_leased_comm')
            ->select(["*",
                  DB::raw("case tbl_leased_comm.user_type when '0' then (select CONCAT(first_name,'',last_name) from users where id=tbl_leased_comm.user_id) when '1' then (select party_name from tbl_partys where id=tbl_leased_comm.user_id) end as user_name")])
            ->where('tbl_leased_comm.user_id','=',$party )
            ->where('tbl_leased_comm.user_id','=',$user_id)
            ->orderBy('tbl_leased_comm.updated_at', 'desc')
            ->get();

非常感谢..

引用链接:Convert mysql query logic to Laravel query builder

关于php - 如何将 select 中的内部查询转换为 Eloquent laravel 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45182624/

相关文章:

php - 部分获取有序行

php - 使用 PHP 备份数据库

java - Hibernate JDBC 批量大小不起作用

javascript - 未触发Typeahead JS自定义事件

PHP 到 Google WebApp 返回 "<H1>Moved Temporarily</H1>The document has moved"

php - Mysql:使用更新设置列的特定值

python - 使用 update() 函数后不使用 session 的 SqlAlchemy 提交

php - 在特定查询中对同一字段的结果集进行排序

php - 在 Laravel 4 中自动注册所有 Controller

php - 在生产环境中运行的应用程序中 Laravel 页面为空白