简体   繁体   中英

MySQL : only_full_group_by multiple date isn't in group by

How is the correct query when only_full_group_by activated?

SELECT
    MAX(DATE (`date`)) AS `date_full`,
    WEEKDAY(`date`) AS `date_week`,
    COUNT(`id`) AS `visits`
    FROM `xxx_visits_stats` 
    WHERE YEAR(`date`) = YEAR("2017-01-01") 
    GROUP BY `date_week`
    ORDER BY `date` DESC

Without(disabled) only_full_group_by it works!

I think your problem is due to the fact that you're ordering by a column that's not mentioned in the GROUP BY and also not an aggregate.

Try this:

  SELECT MAX(DATE(`date`)) AS `date_full`,
         WEEKDAY(`date`) AS `date_week`,
         COUNT(`id`) AS `visits`
    FROM `xxx_visits_stats` 
   WHERE YEAR(`date`) = YEAR("2017-01-01") 
   GROUP BY WEEKDAY(`date`)
   ORDER BY 1 DESC

That form of ORDER BY means use the first column in the SELECT clause for ordering.

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