我在laravel雄辩的查询中遇到此错误:
“ SELECT列表的表达不在GROUP BY子句中,并且包含未聚合的列”
$data = DB::table('passages')
->select('passages.*','users.name','users.family','valid_users.user_id','valid_users.valid_card_id','valid_users.valid_card_decimal','valid_users.valid_card_hex')
->join('valid_users','valid_users.id','=','passages.valid_user_id')
->join('users','valid_users.user_id','=','users.id')
->join('accounts','accounts.user_id','=','users.id')
->whereIn('accounts.role_id',$roles)
->groupBy('valid_user_id')
->get();
答案 0 :(得分:2)
无论何时我们使用Group by子句,那么我们选择的其余列都必须带有aggegate函数(例如:min,max,avg,sum)。
示例:
select sum(t.name),t.age
from table1 t
group by t.age
答案 1 :(得分:0)
查找最后一行的一种方法是使用一个相关的子查询,该查询标识了last(假设您有办法这样做),在此示例中,last被标识为最大auto_incremented id
drop table if exists t;
create table t( id int auto_increment primary key, userid int, val varchar(10));
insert into t (userid,val) values
(1,100),(2,10),(1,15),(2,20);
select t.*
from t
where id = (select max(id) from t t1 where t1.userid = t.userid);
+----+--------+------+
| id | userid | val |
+----+--------+------+
| 3 | 1 | 15 |
| 4 | 2 | 20 |
+----+--------+------+
2 rows in set (0.00 sec)