简体   繁体   中英

MySQL ONLY_FULL_GROUP_BY enabled with CONCAT

I have a table in a database mysql (5.7.21) like this:

+----------+--------------+-----------+-----------+
| id_price | id_reference | price_usd | unix_time |
+----------+--------------+-----------+-----------+

And I need to extract the average price (price_usd) grouped by week of year, or month (unix_time).

I prepare this query:

SELECT CONCAT(WEEKOFYEAR(FROM_UNIXTIME(unix_time)),
  '-',
  YEAR(FROM_UNIXTIME(unix_time))) as date,
  AVG(price_usd) AS "model"
FROM price_avg
INNER JOIN reference ON reference.id_reference=price_avg.id_reference
WHERE price_avg.id_reference=1
GROUP BY WEEKOFYEAR(FROM_UNIXTIME(unix_time)), 
  YEAR(FROM_UNIXTIME(unix_time)),
  price_avg.id_reference
ORDER BY unix_time ASC 

The inner join is useful to get the name of the product having the the id.

I get this error:

#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'name_of_db.price_avg.unix_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

I cannot change the settings of MySQL (I can't disable ONLY_FULL_GROUP_BY mode or anything else).

How do I have to change the query to extract the data in MySQL 5.7.21?

Thanks in advance.

You can use sub query so that it will be a full group by.

Select `date`, 
        AVG(price_usd) AS "model"
From (
   SELECT CONCAT(WEEKOFYEAR(FROM_UNIXTIME(unix_time)),
      '-',
    YEAR(FROM_UNIXTIME(unix_time))) as `date`,
    price_usd
FROM price_avg
INNER JOIN reference ON reference.id_reference=price_avg.id_reference
WHERE price_avg.id_reference=1
) t
GROUP BY `date`
ORDER BY substring(`date`, -4), substring(`date`, 1, 2) ASC;

Result:
date    model
48-1998 11.99
36-2001 19.99

I solved with this query:

SELECT DATE_FORMAT(FROM_UNIXTIME( unix_time ),'%Y-%m') as date, AVG(price_usd) AS model FROM price_avg INNER JOIN reference ON reference.id_reference=price_avg.id_reference WHERE price_avg.id_reference=1 GROUP BY date, price_avg.id_reference ORDER BY date ASC

I have week and year inverted but I can resolve in client enviroment!

Thank you all.

Hit this same problem, reported it as a possible bug on MySQL: https://bugs.mysql.com/bug.php?id=90792&thanks=4

From the initial response, it sounded like it might be treated as a bug and fixed, but I think the follow-up suggests that GROUP BY expressions (instead of columns) aren't part of the SQL standard, and that determining if a complex expression is completely derived from GROUPed expressions is difficult and is, for now at least, something they decided not to pursue: https://mysqlserverteam.com/when-only_full_group_by-wont-see-the-query-is-deterministic/

There are some workarounds in the meantime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM