在表

时间:2016-01-16 19:34:46

标签: php mysql

我有以下声明

SELECT whatweek, username, team FROM weekpicks GROUP BY team, username HAVING count(*) > 1 ORDER BY 'whatweek' asc

输出将在表中找到用户的所有重复项,但列表不会仅输出第二次出现的重复项。有时会发布第1次,其他则是第2次发生。我需要它只发布第二次出现。

1 个答案:

答案 0 :(得分:0)

您可以在子查询中计算所需的周数

    SELECT (
        SELECT whatweek 
        FROM weekpicks t
        WHERE t.username = weekpicks.username 
        AND t.team = weekpicks.team
        ORDER BY whatweek DESC 
        LIMIT 1) AS `needed_whatweek`, 
        username, team 
    FROM weekpicks 
    GROUP BY team, username 
    HAVING count(*) > 1 

编辑或者使用MAX(),我认为它的前提是:

SELECT MAX(whatweek), username, team FROM weekpicks 
GROUP BY team, username HAVING count(*) > 1