时间:2017-05-17 11:05:20

标签: mysql sql

我有这个查询将我的网站上的文章作者组合在一起,以及他们最近做了多少文章以及他们最近发布的日期。

问题是这个错误:

  

#1055 - SELECT列表的表达式#4不在GROUP BY子句中   包含非聚合列' goltest.a.author_id'不是   功能上依赖于GROUP BY子句中的列;这是   与sql_mode = only_full_group_by

不兼容
SELECT
    COUNT(DISTINCT a.article_id) AS `counter`,
    u.username,
    u.user_id,
    (
    SELECT
        `date`
    FROM
        `articles`
    WHERE
        `author_id` = a.`author_id`
    ORDER BY
        `article_id`
    DESC
LIMIT 1
) AS `last_date`
FROM
    `articles` a
LEFT JOIN
    `users` u
ON
    u.user_id = a.author_id
LEFT JOIN
    `article_category_reference` c
ON
    a.`article_id` = c.`article_id`
WHERE
    a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
    u.`username`,
    u.`user_id`
ORDER BY
    `counter`
DESC
    ,
    a.date
DESC

不要理解为什么要这样做&#34; author_id&#34;在列表中,因为我没有选择它?

1 个答案:

答案 0 :(得分:1)

对于那些感兴趣的人,我用它修复了它,用日期的MAX()值替换了子查询:

SELECT
    COUNT(DISTINCT a.article_id) AS `counter`,
    u.username,
    u.user_id,
    MAX(a.date) AS `last_date`
FROM
    `articles` a
LEFT JOIN
    `users` u
ON
    u.user_id = a.author_id
LEFT JOIN
    `article_category_reference` c
ON
    a.`article_id` = c.`article_id`
WHERE
    a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
    u.`username`,
    u.`user_id`
ORDER BY
    `counter`
DESC
    ,
    last_date
DESC

感谢Shadow指出“max”:)