我在Laravel 5.4上遇到问题,当我尝试只使用一个连接时,它可以正常工作并返回正确的数据,但是他们添加了另一个不起作用的连接。
$data = Player::select(DB::raw('CONCAT(familyName,", ",firstName) AS fullName'))
->where('firstname', 'like', '%'.$search.'%')
->orWhere('familyName', 'like', '%'.$search.'%')
->orderBy('familyName', 'asc')
->join('teams', 'players.primaryClubId', '=', 'teams.clubId')
->join('person_competition_statistics', 'players.personId', '=', 'person_competition_statistics.personId')
->addSelect(['players.*', 'teams.teamName', 'teams.teamNickname', 'teams.teamCode'])
->get()
->unique() //remove duplicates
->groupBy(function($item, $key) { //group familyName that starts in same letter
return substr($item['familyName'], 0, 1);
})
->map(function ($subCollection) {
return $subCollection->chunk(4); // put your group size
});
return $data;
返回错误:
QueryException in Connection.php line 647:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'familyName' in field list is ambiguous (SQL: select CONCAT(familyName,", ",firstName) AS fullName, `players`.*, `teams`.`teamName`, `teams`.`teamNickname`, `teams`.`teamCode` from `players` inner join `teams` on `players`.`primaryClubId` = `teams`.`clubId` inner join `person_competition_statistics` on `players`.`personId` = `person_competition_statistics`.`personId` where `firstname` like %% or `familyName` like %% order by `familyName` asc)
答案 0 :(得分:1)
如果你要加入表格,那么你应该给表别名。比如team as t,player为p,然后是列名,如p.playername