Laravel: 1066 Not unique table/alias

11,849

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  
Share:
11,849
user3664594
Author by

user3664594

Updated on June 22, 2022

Comments

  • user3664594
    user3664594 over 1 year

    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?

  • user3664594
    user3664594 over 9 years
    ok now I get Integrity constraint violation: 1052 Column 'council' in where clause is ambiguous
  • user3664594
    user3664594 over 9 years
    i still get this 1052 Column 'council' in where clause is ambiguous , is it because we're joining the suburb_near table twice and the council column is joined twice even though we're only selecting it once?
  • user3664594
    user3664594 over 9 years
    ->where('suburb_near_one.council', 'like', '%'. $input['council'].'%') this was the problem