简体   繁体   中英

How to combine two queries in laravel?

query1:

 $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:

$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.

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

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:

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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