我的控制器中有两个查询
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');
}
如果我独立运行,他们运行正常。但如果我一起跑,我得到一个错误: 1066不是唯一的表/别名
我该如何更改?
答案 0 :(得分:3)
使用表别名(as
)
第一个查询如下:
$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');
第二个查询如下:
$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