【问题标题】:Convert MySQL query to Laravel (5.5) Query Builder将 MySQL 查询转换为 Laravel (5.5) 查询生成器
【发布时间】:2018-01-26 17:00:58
【问题描述】:

我在将以下 MySQL 查询转换为 Laravel (5.5) Eloquent 查询生成器时遇到问题。

$start = '2018-01-22'; // Some random starting point for the Query

$query = "SELECT * FROM cdr c WHERE soort=3 AND con_duur > 0 AND con_duur
>= (select kortbel_seconden from queue q where q.queue_id=c.queue_id) AND `start_tijd >= '$start'";`

我有以下型号:

// CDR 模型

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CDR extends Model
{
    protected $table = 'cdr';
    protected $primaryKey = 'cdr_id';

    public function Queue()
    {
        return $this->hasOne('App\Models\Queue', 'queue_id', 'queue_id');
    }
}

// 队列模型

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Queue extends Model
{

    protected $table = 'queue';

    protected $primaryKey = 'queue_id';

    public function cdr()
    {
        return $this->belongsTo('App\Models\CDR', 'queue_id', 'queue_id');
    }
}

到目前为止,我的控制器中有以下代码:

App\Models\CDR::with('queue')
    ->where('soort', '3')
    ->where('con_duur', '>', '0')
    ->where('start_tijd', '>=' , $start)
    ->where('con_duur', '>=', ' ') // this is where the sub select from the `queue` table should be : (select kortbel_seconden from queue q where q.queue_id=c.queue_id)
    ->get();

我被子选择卡住了,有没有办法用 Laravel 的查询生成器来做到这一点?

谢谢!

【问题讨论】:

  • 你想使用 DB:raw() 方法吗?请在此处查看原始表达式。 laravel.com/docs/5.5/queries#raw-expressions 第一个例子可能对你有帮助。
  • @Kristiyan 感谢您的回复。我不介意使用 DB::raw() 方法。我仍然不明白这将如何工作。我去过 Laravel 文档并且知道 DB::Raw(),但它仍然是关系表上的一个选择。我已经用 join() 解决了另一个查询,但是 CDR 表可能会变得非常大,我不想吃掉所有的内存。

标签: php mysql laravel-5


【解决方案1】:

考虑这段代码:

        DB::table('cdr AS c')
            ->select("*")
            ->where('c.soort','=',3)
            ->where('c.con_duur','>',0)
            ->where('c.con_duur','>=',function($query){

                $query->select('kortbel_seconden')
                    ->from('queue AS q')
                    ->where('q.queue_id', '=', 'c.queue_id');

            })
            ->where("c.start_tijd",">=",$start)
            ->get();

这部分查询:

->where('c.con_duur','>=',function($query){

                $query->select('kortbel_seconden')
                    ->from('queue AS q')
                    ->where('q.queue_id', '=', 'c.queue_id');

            })

用于实现以下部分的查询:

`c`.`con_duur` >= 
  (SELECT 
    kortbel_seconden 
  FROM
    queue q 
  WHERE q.queue_id = c.queue_id)

上面的查询结果也可以通过下面的查询得到,也可以通过join:

 DB::table('cdr AS c')
            ->select("c.*")
            ->join('queue AS q', 'q.queue_id', '=', 'c.queue_id')
            ->where('c.soort','=',3)
            ->where('c.con_duur','>',0)
            ->where('c.con_duur','>=','q.kortbel_seconden')
            ->where("c.start_tijd",">=",$start)
            ->get();

更多详情您可以访问:

https://laravel.com/docs/5.5/queries#where-clauses

【讨论】:

  • 为什么?您能否在该代码中添加一些解释,说明为什么它可以解决问题?
  • ->where('c.con_duur','>=',function($query){ $query->select('kortbel_seconden') ->from('queue AS q') - >where('q.queue_id', '=', 'c.queue_id'); }) 这部分查询等价于c.con_duur >= (SELECT kortbel_seconden FROM queue AS @ 987654331@ WHERE q.queue_id = c.queue_id) 更多信息,您可以访问laravel.com/docs/5.5/queries#where-clauses的官方文档
  • 请将此类解释添加到答案本身,而不是评论部分
猜你喜欢
  • 2015-12-13
  • 2019-03-09
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2017-12-09
  • 2021-08-07
  • 2017-12-29
  • 2016-08-10
相关资源
最近更新 更多