【问题标题】:Convert mysql query to working with laravel将 mysql 查询转换为使用 laravel
【发布时间】:2018-11-07 00:19:12
【问题描述】:

我有这个对我来说很好的 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

我该怎么做?

【问题讨论】:

    标签: mysql laravel-5 eloquent laravel-query-builder


    【解决方案1】:

    要使用 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多