Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中编写这个复杂的查询_Php_Mysql_Sql_Laravel_Query Builder - Fatal编程技术网

Php 如何在Laravel中编写这个复杂的查询

Php 如何在Laravel中编写这个复杂的查询,php,mysql,sql,laravel,query-builder,Php,Mysql,Sql,Laravel,Query Builder,这是我的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

这是我的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();

祝你好运

是否来自(选择i.store\u id…零件缺少关闭
?否,括号关闭[->其中原始('si.model\u id=i.model\u id和si.store\u id=i.store\u id和si.status=1订单由si.created\u在描述限制1下创建)作为库存])在限制后的这一部分中,1查询是正确的,但我不明白如何在结尾处使用2个结束括号。这就是我得到的错误,有时使用纯sql更容易。。有一个静态方法DB::statement(),我尝试使用DB::select语句,效果很好。但是如何使用分页呢?我不能将paginate()方法与DB::select方法一起使用。