我試圖從user_groups數據透視表中的某個用戶組中匹配users表中的所有用戶。即時通訊從Cartalyst btw使用Sentry 2。Laravel - 帶連接和concat的Querybuilder
這可以讓所有用戶名字和姓氏連接在一起。
User::select(DB::raw('CONCAT(last_name, ", ", first_name) AS full_name'), 'id')
->where('activated', '=', '1')
->orderBy('last_name')
->lists('full_name', 'id');
當我嘗試將其更改爲也過濾不屬於某個組的用戶時出現語法錯誤。
User::select(DB::raw('SELECT CONCAT(user.last_name, ", ", user.first_name) AS user.full_name'), 'user.id', 'users_groups.group_id', 'users_groups.user_id')
->join('users_groups', 'user.id', '=', 'users_groups.user_id')
->where('user.activated', '=', '1')
->where('users_groups.group_id', '=', $group)
->orderBy('user.last_name')
->lists('user.full_name', 'user.id');
任何在正確的方向微調將不勝感激。
編輯:語法錯誤
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'SELECT CONCAT(user.last_name, ", ",
user.first_name) AS user.full_name, `user`.`' at line 1 (SQL: select SELECT
CONCAT(user.last_name, ", ", user.first_name) AS user.full_name, `user`.`id`,
`users_groups`.`group_id`, `users_groups`.`user_id` from `users` inner join
`users_groups` on `user`.`id` = `users_groups`.`user_id` where
`users`.`deleted_at` is null and `user`.`activated` = 1 and
`users_groups`.`group_id` = 9 order by `user`.`last_name` asc)
你可以添加語法錯誤? – 2014-10-10 20:06:59
將錯誤添加到問題中。謝謝 – MDS 2014-10-10 20:12:04