skip to Main Content

I’m trying to connect a relation table, and and get the max value

$query->joinWith('hotelPrices')
  ->andWhere([HotelPrice::tableName() . '.price' => $form->prices])
  ->max(HotelPrice::tableName() . '.price');

but i get this error

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: ‘hotel_price’

how to me in this case to use alias?

2

Answers


  1. The error message indicates that you have used the same table alias (‘hotel_price’) in multiple places in your query. You can use a different alias for the ‘hotel_prices’ table in your join statement.

     $query->joinWith(['hotelPrices' => function($query) {
     $query->from(['hp' => HotelPrice::tableName()]);
     }])
     ->andWhere(['hp.price' => $form->prices])
     ->max('hp.price');
    
    Login or Signup to reply.
  2. As stated in documentation, you can add alias by adding it to relation name. In your case:

    $query->joinWith('hotelPrices hp')
        ->andWhere(['hp.price' => $form->prices])
        ->max('hp.price');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search