MySQL查询表中某个字段的重复数据
1. 查询SQL表中某个字段的重复数据
SELECT user_name,COUNT(*) AS count FROM db_user_info GROUP BY user_name HAVING count > 1;
SELECT user_name FROM db_user_info GROUP BY user_name HAVING COUNT(1) > 1;
SELECT * FROM db_user_info WHERE user_name IN (SELECT user_name FROM db_user_info GROUP BY user_name HAVING COUNT(1) > 1);
- HAVING是分组(GROUP BY)后的筛选条件,分组后的数据组内再筛选;WHERE 则是在分组前筛选。
- WHERE 子句中不能使用聚集函数,而 HAVING 子句中可以,所以在集合函数中加上了 HAVING 来起到测试查询结果是否符合条件的作用。即 HAVING 子句的适用场景是可以使用聚合函数。