首页 > 数据库 > SQL Server >

sql查询重复数据

2017-06-29

查询表t_contract_purchase中,所有purcontractid字段内容重复的数据,并把数据按照先按照purcontractid,再按照purcharseid 降序排列展示出来。

select * from t_contract_purchase t where t.purcontractid  in (select purcontractid from t_contract_purchase
  group by purcontractid having count(1)>1) and t.subcompanyid='****' order by t.purcontractid desc, t.purcharseid desc;

查询表t_contract_purchase中,所有purcontractid字段内容重复的数据,并把数据按照先按照purcontractid,再按照purcharseid 降序排列展示出来。

更加精简一点,可以将语句改成:

select * from t_contract_purchase t where (select count(*) from t_contract_purchase s where s.purcontractid=t.purcontractid)>1
相关文章
最新文章
热点推荐