Laravel: Paginating a query on a view MySQL Error 1140 Mixing of GROUP columns

南楼画角 提交于 2021-01-05 08:45:08

问题


I am trying to do a simple pagination of a query that uses a view:

DB::table('vw_myview')->paginate(50)

And get this error:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select count(*) as aggregate from vw_myview)

Yet when I manually run the query, it works just fine:

mysql> select count(*) as aggregate from vw_myview;
+-----------+
| aggregate |
+-----------+
|       776 |
+-----------+
1 row in set (0.20 sec)

After searching around I found that I should just have to add a GROUP BY clause (just like the error says) and I have tried doing that with no luck:

DB::table('vw_myview')->groupBy('column_on_view')->paginate(50)

SQLSTATE[42000]: Syntax error or access violation: 1055 'database_name.table_used_by_view.column_on_table' isn't in GROUP BY (SQL: select count(*) as aggregate from vw_myview group by column_on_view)

Okay lets try grouping by a column on one of the tables the view uses:

DB::table('vw_myview')->groupBy('table_used_by_view.column_on_table')->paginate(50)

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'database_name.table_used_by_view.column_on_table' in 'group statement' (SQL: select count(*) as aggregate from vw_myview group by database_name.table_used_by_view.column_on_table)

So why does the query work when I run it manually but fails when run through Eloquent? Any help would be greatly appreciated.


回答1:


You know, I am thinking your Mysql server has the sql_mode = ONLY_FULL_GROUP_BY turned on... Can you check this... (from mysql ... select @@sql_mode; If it is set, turn it off... and try your query again... I think it shall pass... Mysql 5.7xxx has this turned on by default now...



来源:https://stackoverflow.com/questions/47044851/laravel-paginating-a-query-on-a-view-mysql-error-1140-mixing-of-group-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!