1、表中存的数据如下图,a,b是重复数据

mysql 只查询没有重复的数据 mysql查询不重复数据数量_mysql 只查询没有重复的数据


2、 我们要查询出的数据如下图

mysql 只查询没有重复的数据 mysql查询不重复数据数量_sql_02


3、首先查出重复数据的name,再查出重复数据的id,最后子查询出不重复的数据

(1)SELECT name from test GROUP BY name HAVING count(name) > 1
 (2)SELECT id from test WHERE name in (SELECT name from test GROUP BY name HAVING count(name) > 1)
 (3)SELECT * from test WHERE id not in
 (SELECT id from test WHERE name in (SELECT name from test GROUP BY name HAVING count(name) > 1))