簡體   English   中英

左連接內具有多個條件的高級WHERE子句無法使用Eloquent

[英]Advanced WHERE clause inside left join with multiple condition doesn't work using Eloquent

我正在嘗試使用雄辯的ORM生成以下SQL查詢

select * from table_1 
left join table_2 on table_2.id = table_1.id 
    and table_2.offline <= ? 
    and (
        table_2.online >= ? or table_2.online= ?
    )

盡管在stackoverflow和其他站點中采用了建議的不同方法來在AND-0R子句之間獲取括號(),但我無法獲得所需的結果。 口才查詢示例:

        Capsule::table('table_1')
        ->leftJoin('table_2', function ($join) use ($from, $until) {
            $join->on('table_2.id', '=', 'table_1.id')
                ->where('table_2.offline', '<=', $until)
                ->where(function($q) use ($from) {
                    $q->where('table_2.online', '>=', $from)
                        ->orWhere('table_2.online', '=', '0000-00-00 00:00:00');
                });
        })
        ->toSql();

這是我目前從上述口才查詢作為原始sql獲取的信息。

select * from `table_1` 
left join `table_2` 
    on `table_2`.`id` = `table_1`.`id` 
    and `table_2`.`offline` <= ? and   ?

整個Advanched where子句更改為?。 任何建議都歡迎!

在Laravel 5中,您應該通過以下方式啟用查詢日志記錄:

DB::enableQueryLog();

然后,在發表最終聲明后:

DB::table('table_1')
        ->leftJoin('table_2', function ($join) use ($from, $until) {
            $join->on('table_2.id', '=', 'table_1.id')
                ->where('table_2.offline', '<=', $until)
                ->where(function($q) use ($from) {
                    $q->where('table_2.online', '>=', $from)
                        ->orWhere('table_2.online', '=', '0000-00-00 00:00:00');
                });
        })
        ->get();

趕緊跑:

dd(DB::getQueryLog());

它將返回SQL和最后執行的查詢的綁定。 在Laravel 4中,默認情況下啟用queryLogging,因此您可以轉儲查詢日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM