簡體   English   中英

如何在Laravel中編寫此復雜查詢

[英]How to write this complex query in Laravel

這是我的SQL查詢

select sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* from `inventory` as `i` left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id` left join `model` as `m` on `m`.`id` = `ms`.`model_id` where `i`.`model_id` = m.id and `m`.`status` = 1 and `ms`.`status` = 1 and `i`.`created_at` = (select max(si.created_at) from `inventory` as `si` where si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock group by `stock`.`model_id` 

基本上,我試圖在最近的日期檢索特定商店的庫存。

我在laravel中編寫了以下查詢:

$results = DB::table('inventory as i')
                      ->selectRaw( 'sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* ')               
                      ->leftJoin('model_store as ms','ms.store_id','=','i.store_id')
                      ->leftJoin('model as m','m.id','=','ms.model_id')
                      ->where('i.model_id','=', 'm.id')
                      ->where('m.status','=', 1)
                      ->where('ms.status','=', 1)
                      ->where('i.created_at','=',function($query){
                          $query->from('inventory as si')
                                ->selectRaw('max(si.created_at)')
                                ->whereRaw('si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock ')
                                ->groupBy('stock.model_id');
                          })->get();

這給了我以下錯誤:

 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 
(SQL: select sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* from `inventory` as `i` left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id` left join `model` as `m` on `m`.`id` = `ms`.`model_id` where `i`.`model_id` = m.id and `m`.`status` = 1 and `ms`.`status` = 1 and `i`.`created_at` = (select max(si.created_at) from `inventory` as `si` where si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock group by `stock`.`model_id`)) 

我需要應用分頁。 因此,我必須如上所述編寫此查詢。 我嘗試使用DB::select()編寫查詢。 它返回正確的記錄,但是不幸的是,我不能對此使用分頁。

請幫助我在laravel中編寫正確的查詢。

嘗試讓PDO實例直接執行查詢,如下所示:

    $PDO=DB::connection('mysql')->getPdo();
    $stmt=$PDO->prepare("
        select 
            sum(stock.total_in_stock) as total_in_stock,
            stock.name,
            stock.inventory_id
        FROM
            (SELECT 
                i.store_id,
                    i.model_id,
                    i.total_in_stock,
                    i.id as inventory_id,
                    m . *
            from
                `inventory` as `i`
            left join `model_store` as `ms` ON `ms`.`store_id` = `i`.`store_id`
            left join `model` as `m` ON `m`.`id` = `ms`.`model_id`
            where
                `i`.`model_id` = m.id
                    and `m`.`status` = 1
                    and `ms`.`status` = 1
                    and `i`.`created_at` = (select 
                        max(si.created_at)
                    from
                        `inventory` as `si`
                    where
                        si.model_id = i.model_id
                            AND si.store_id = i.store_id
                            AND si.status = 1
                    ORDER BY si.created_at DESC
                    LIMIT 1)) as stock
        group by `stock`.`model_id`

    ");

    // you can use prepared statments too,
    // you will need to place :status
    // accordingly in your query

   //$stmt->bindParam(':status', 1);


    $stmt->execute();

    $result = $stmt->fetchAll();

祝好運!

暫無
暫無

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

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