Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: 1066 Not unique table/alias

I have two queries in my controller

if (isset($input['council']) && $input['council'] != '')
{
  $query = $query->join('suburb_near', 'titles.suburb', '=', 'suburb_near.suburb')
    ->select(array('titles.*', 'suburb_near.suburb' , 'suburb_near.council'))
    ->where('council', 'like', '%'. $input['council'].'%')
    ->orderBy('views', 'desc');
}
if (isset($input['country']) && $input['country'] != '')
{
  $query = $query->join('suburb_near', 'titles.suburb', '=', 'suburb_near.suburb')
    ->select(array('titles.*', 'a.suburb' , 'suburb_near.country'))
    ->where('country', 'like', '%'. $input['country'].'%')
    ->orderBy('views', 'desc');
}

If I run either independently they run fine. But if I run both together, I get an error: 1066 Not unique table/alias

How should I change this?

like image 320
user3664594 Avatar asked Jun 04 '14 08:06

user3664594


1 Answers

Use table alias (as)

The first query would look like:

$query = $query
   ->join('suburb_near as suburb_near_one', 'titles.suburb', '=', 'suburb_near_one.suburb') 
   ->select(array('titles.*', 'suburb_near_one.suburb' , 'suburb_near_one.council'))
   // Edit after user3664594s comment 
   ->where('suburb_near_one.council', 'like', '%'. $input['council'].'%')
   ->orderBy('views', 'desc');

The second query would look like:

$query = $query
    ->join('suburb_near as suburb_near_two', 'titles.suburb', '=', 'suburb_near_two.suburb')
    ->select(array('titles.*', 'a.suburb' , 'suburb_near_two.country'))
    // the rest of the query  
like image 165
toesslab Avatar answered Sep 24 '22 22:09

toesslab