简体   繁体   English

MySQL:only_full_group_by多个日期不在分组依据中

[英]MySQL : only_full_group_by multiple date isn't in group by

How is the correct query when only_full_group_by activated? 激活only_full_group_by时如何正确查询?

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! 没有(禁用)only_full_group_by可以正常工作!

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. 我认为您的问题是由于您按的是GROUP BY中未提及的列进行排序,而且该列也不是合计的。

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. 这种形式的ORDER BY意味着使用SELECT子句中的第一列进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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