求SQL写法:在表中求出一字段有相等值的纪录.

初学SQL,我的表中有一个字段中有很多数据是相等的,也就是这个字段有重复值,有重复的值有多种,每一种又有多个,请问怎样把这个字段有重复值的相关纪录分组查出来,把这个字段有唯一值的纪录过滤掉,谢谢!
---------------------------------------------------------------

select a.* from tablename as a,(
select fieldname from tablename group by fieldname
having count(*) >1
) as b
where a.fieldname=b.fieldname
order by a.fieldname

---------------------------------------------------------------

select yourtable.* from yourtable where exists (select 1 from yourtable x where yourtable.fieldname=x.fieldname group by fieldname having count(*)>1)

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus