【问题标题】:MySQL query with two INNER JOIN returns duplicate entries in the result具有两个 INNER JOIN 的 MySQL 查询在结果中返回重复条目
【发布时间】:2012-10-21 02:01:21
【问题描述】:

我有以下数据结构:文章有 m:n 个用户

共有三个表:articlesusersarticles_users(链接表)

现在我需要这个查询:给我所有最近写过文章的用户

不幸的是,这个查询返回了重复的结果:

SELECT  DISTINCT users.id, 
        users.username, 
        articles.published, 
        articles_users.user_id
FROM    users 
        INNER JOIN articles_users 
            ON users.id = articles_users.user_id
        INNER JOIN articles     
            ON articles.id = articles_users.article_id
ORDER BY articles.published DESC
LIMIT 25;

结果:

id      username        published   user_id
113     silva_mihat     2012-10-30  112
228     paula_tille     2012-10-27  258
228     paula_tille     2012-10-26  258
631     andrea_gurkow   2012-10-24  631
275     hubert_mayer    2012-10-24  275
198     annette_mulger  2012-10-22  198
255     uta_zuffter     2012-10-22  235
and so on ...

有人知道为什么 DISTINCT 在这里不起作用吗?

【问题讨论】:

    标签: mysql group-by duplicates distinct inner-join


    【解决方案1】:

    因为DISTINCT 适用于整行(不仅仅是users.id 本身)。如您所见,所有返回的行都不是唯一。尝试这样的事情,子查询背后的想法是它为每个article_id获取最近的published日期

    SELECT  users.id, 
            users.username, 
            articles.published
    FROM    users 
            INNER JOIN articles_users 
                ON users.id = articles_users.user_id
            INNER JOIN articles 
                ON articles.id = articles_users.article_id
            INNER JOIN 
            (
                SELECT id, MAX(published) maxDate
                FROM articles
                GROUP BY id
            ) c ON  articles.id = c.ID AND 
                    articles.published = c.maxDATE
    -- ORDER BY articles.published DESC
    -- LIMIT 25
    

    【讨论】:

    • 我的一个朋友刚刚给了我这个解决方案:SELECT users.id, users.username, max(articles.published), articles_users.user_id FROM users INNER JOIN articles_users ON users.id = articles_users.user_id INNER JOIN articles ON articles_users.article_id = articles.id GROUP BY articles_users.user_id ORDER BY max(articles.published) DESC LIMIT 25
    • 它能给你准确的结果吗?也许它会工作一段时间,但如果你想添加一些特定的列,例如(articlename,或任何。)它不会给你正确的结果。
    • 是的。结果是正确的。我想要的只是“给我所有最近写过文章的用户”
    • 好吧,好吧 :D 我只是在反其道而行之。呵呵
    【解决方案2】:

    这应该按作者而不是按文章分组。

    select
      users.id,
      users.username,
      maxPublished
    from users
    inner join (
      select
        max(articles.published) as maxPublished,
        articles_users.user_id as userID
      from articles
      join articles_users on articles_users.article_id = articles.id
      group by articles_users.user_id
    ) as p on users.id = userID
    order by maxPublished desc
    limit 25
    ;
    

    【讨论】:

    • 感谢您的快速回答。另一种解决方案是:SELECT users.id, users.username, max(articles.published), articles_users.user_id FROM users INNER JOIN articles_users ON users.id = articles_users.user_id INNER JOIN articles ON articles_users.article_id = articles.id GROUP BY articles_users.user_id ORDER BY max(articles.published) DESC LIMIT 25
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多