简体   繁体   English

如何在laravel中合并两个查询?

[英]How to combine two queries in laravel?

query1: 查询1:

 $image= DB::table('products_photos')->groupBy('filename')
->get(['filename', DB::raw('MAX(id) as id')]);

This is my query to retreive single image for all the records stored. 这是我查询以检索单个图像的所有记录。

query2: 查询2:

$verifiedValues= priceInfo::join('productDescription', 
'productDescription.productId', '=', 'productPriceDetails.productId')
   ->join('productAdditionalInformation', 
 'productAdditionalInformation.productId', '=', 
'productPriceDetails.productId')
   ->join('products_photos', 'products_photos.productId', '=', 
'productAdditionalInformation.productId')
 ->select('productPriceDetails.SKUID','productDescription.modelName')
   **->select($image)**
        ->where('productPriceDetails.nonliveStatus', '=', 
  "QCVerified")-

>where('productAdditionalInformation.nonliveStatus','=',"QCVerified")
      ->where('productDescription.nonliveStatus','=',"QCVerified")
      ->where('products_photos.nonliveStatus','=',"QCVerified")
      ->where('productPriceDetails.listingStatus','=',"Inactive") 
     ->get();

This is my query where I have joined multiple tables and need to combine the query1 with query2. 这是我已连接多个表的查询,需要将query1与query2组合在一起。

SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters

error on combining both the queries 合并两个查询时出错

IMHO, you have two options. 恕我直言,您有两种选择。 Either you try to merge the two resulting collections by using the merge() method, as it is explained via Laravel :: Combine two DB query objects 您可以尝试使用merge()方法合并两个结果集合,如Laravel中所述:::合并两个数据库查询对象

Or you use a join to combine the two queries in one single query. 或者,您可以使用联接将两个查询合并为一个查询。 To keep your Builder object (without get() ), you may use something like this: 要保留您的Builder对象(不使用get() ),可以使用如下所示的内容:

$single_query = DB::table('first_query')
    ->join(DB::raw('('.$second_query->toSQL().') alias'), 'id', 'alias.id')
    ->mergeBindings($second_query)
    ->get();

You may omit the mergeBindungs() clause, if you dont have any parameters. 如果没有任何参数,则可以省略mergeBindungs()子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM