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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With