Skip to content
Advertisement

What is the best way to do the job of Group By in mysql when sql_mode=only_full_group_by

I want to perform a query like to get the last record of any type in DB, at my localhost, I use Maria Db and the query is as follow:

JavaScript

group is a column which I save type in it, for instance: marketing, blog, order, etc

This query works fine on local, but on the server I get the following error:

JavaScript

According to MySQL document I can use the following command to make this possible:

JavaScript

But I don’t have the sufficient privilege on Db and get the following error:

JavaScript

I asked the hosting to do this for me, they replied that they don’t want to do this action

I use the YII2 framework, and now I want a way to add this on the option of database_config.php of the framework or change the query to something else with the same result, and not losing performance

Advertisement

Answer

ONLY_FULL_GROUP_BY is a good thing, which enforces basic ANSI SQL rules. Don’t change it, fix your code instead.

From there one: you want entire records, so you should not think aggregation, but filtering.

Then: in a database table, records are unordered; for your question to just make sense, you need a column that defines the ordering of rows in each group, so it unambiguous what “last” record mean. Let me assume that you have such column, called id.

Here is a typical approach at this top-1-per-group problem, using a correlated subquery for filtering:

JavaScript

Alternatively, if you are running MySQL 8.0, you can use window functions:

JavaScript

Side note: group is a language keyword, hence a poor choice for a column name. I renamed it to grp in the queries.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement