在mysql中显示重复记录

时间:2012-09-10 19:08:37

标签: mysql duplicate-data

我有一个表(user_admin_password)。我正在使用此查询来获取记录。

SELECT user_key,admin_status,count(*) 
FROM user_admin_status 
GROUP BY 
   user_key,admin_status having count(*) > 1 
ORDER BY user_key,admin_status;

结果是:

+----------+--------------+----------+
| user_key | admin_status | count(*) |
+----------+--------------+----------+
|        1 | NON-DBA      |        5 |
|        3 | DBA          |      328 |
|        5 | NON-DBA      |        8 |
|        6 | NON-DBA      |       25 |
|        7 | NON-DBA      |        4 |
|        9 | DBA          |      232 |
|       10 | NON-DBA      |        4 |
|       11 | DBA          |        4 |
|       13 | NON-DBA      |        8 |
|       15 | NON-DBA      |        2 |
|       16 | DBA          |      326 |
|       16 | NON-DBA      |        2 |
|       17 | NON-DBA      |       10 |
|       18 | NON-DBA      |        5 |
|       19 | NON-DBA      |       12 |
|       20 | NON-DBA      |        2 |
|       21 | NON-DBA      |        2 |
...
...

现在,我想要所有带有重复记录的user_keys ...例如16 ...任何建议???

1 个答案:

答案 0 :(得分:3)

将其包装在另一个选择中:

select user_key, count(*) 
from
(
    select user_key,admin_status,count(*) 
    from user_admin_status 
    group by user_key,admin_status 
    having count(*) > 1 
) x
group by user_key
having count(*) > 1