带有两个INNER JOIN的MySQL查询返回结果中的重复条目

时间:2012-10-31 15:00:20

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

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

有三个表:文章用户 articles_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不能在这里工作?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

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
;