将mysql查询转换为使用laravel

9udxz4iz  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(376)

我有一个mysql查询,它对我来说工作得很好。现在我决定将我的项目转换为laravel项目,所以我想将mysql查询转换为laravel查询。
我的问题是:

select c.username,
    max(case when c.attribute = 'Cleartext-Password' then c.value end) as password,
    max(case when c.attribute = 'Expiration' then c.value end) as expiration,
    max(case when c.attribute = 'ChilliSpot-Max-Total-Octets' then c.value end) as quta,
    max(case when c.attribute = 'Simultaneous-Use' then c.value end) as simul,
    max(case when c.attribute = 'Max-All-Session' then c.value end) as session,
    max(c.adsoyad) as realname, min(c.dtarih) as birthdate, min(c.telefon) as phone,min(c.tcno) as tc,max(c.email) as email,min(c.id) as id
from radcheck c 
group by c.username

我该怎么做?

q8l4jmvw

q8l4jmvw1#

要使用laravel查询生成器转换以上内容,需要使用原始表达式

$result = DB::table('radcheck as c')
    ->select('c.username', 
    DB::raw("max(case when c.attribute = 'Cleartext-Password' then c.value end) as password"),
    DB::raw("max(case when c.attribute = 'Expiration' then c.value end) as expiration"),
    DB::raw("max(case when c.attribute = 'ChilliSpot-Max-Total-Octets' then c.value end) as quta"),
    DB::raw("max(case when c.attribute = 'Simultaneous-Use' then c.value end) as simul"),
    DB::raw("max(case when c.attribute = 'Max-All-Session' then c.value end) as session"),
    DB::raw("max(c.adsoyad) as realname"),
    DB::raw("min(c.dtarih) as birthdate"),
    DB::raw("min(c.telefon) as phone"),
    DB::raw("min(c.tcno) as tc"),
    DB::raw("max(c.email) as email"),
    DB::raw("min(c.id) as id")
    )
    ->groupBy('c.username')
    ->get();

相关问题