新增字段IsCheckInvoice
Alter table T_LegalUnit add IsCheckInvoice bit
UPDATE T_LegalUniT SET IsCheckInvoice=1 WHERE LegalUnitID='000010'
SELECT *FROM T_LegalUnit WHERE IsCheckInvoice=1
1.查询表中重复字段
SELECT MOBILE_PHONE FROM T_Saas_Device GROUP BY MOBILE_PHONE,CompanyGUID HAVING COUNT(*)>1
重复数据,保留uid最小的数据
create database Test
use Test
if OBJECT_ID('users','U') is not null
drop table users
go
create table users(
uid int identity(1,1) primary key ,
uname varchar(50) not null,
uemail varchar(50) not null,
sex char(2) not null ,
createtime datetime not null
)
insert into users (uname,uemail,sex,createtime)values('张筱雨','mi@163.com','女',getdate());
insert into users (uname,uemail,sex,createtime)values('王旭','wangxu@163.com','男',getdate());
insert into users (uname,uemail,sex,createtime)values('张晓','zhangxiao@163.com','女',getdate());
insert into users (uname,uemail,sex,createtime)values('刘小雨','xiaoyu@163.com','女',getdate());
insert into users (uname,uemail,sex,createtime)values('李青','liqing@163.com','男',getdate());
insert into users (uname,uemail,sex,createtime)values('肖珂','xiaoke@163.com','女',getdate());
insert into users (uname,uemail,sex,createtime)values('张西','zhangxi@163.com','男',getdate());
insert into users (uname,uemail,sex,createtime)values('程旭','chengxu@163.com','男',getdate());
insert into users (uname,uemail,sex,createtime)values('小明','wang@163.com','男',getdate());
insert into users (uname,uemail,sex,createtime)values('小明','wang@163.com','男',getdate());
select *from users
第一种写法
delete u from users u
where exists(select *from
(select uname from users group by uname having count(uname)>1)as s
where u.uname=s.uname)
and u.uid not in (select min(uid) from users group by uname )
第二种写法
delete from users where uid not in(
select uid from users a where not exists (
select 1 from users b where a.uname = b.uname and b.uid < a.uid ))
查询重复的用户名
select *from users
where uname in(select uname from users group by uname having count(*)>1)
查询重复的用户名次数和记录
select count(*)as 重复次数,uname from users group by uname having count(*)>1
查找表中多余的重复记录,重复记录是根据单个字段(uname)来判断
select *from users
where uname in(select uname from users group by uname having count(uname)>1)
查找表中多余的重复记录(多个字段)
select *from users u where exists
(select *from (select uname,uemail from users group by uname,uemail having count(*)>1)as s
where u.uname=s.uname and u.uemail=s.uemail)
查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select *from users u
where exists(select *from
(select uname,uemail from users group by uname,uemail having count(*)>1)as s
where u.uname=s.uname and u.uemail=s.uemail)
and u.uid not in(select min(uid)from users group by uname,uemail)
两条相同的记录 ,查询只有uname相同的数据
select *from users where uname in(select uname from users group by uname having count(uname)>1)
两条相同的记录 ,查询只有uname,uemail相同的数据
select *from users u where exists(select *from
(select uname,uemail from users group by uname,uemail having count(*)>1 )as s
where u.uname=s.uname and u.uemail=s.uemail)order by uname desc
删除表中多余的重复记录,重复记录是根据单个字段(uname)来判断,只留有uid最小的记录
delete u from users u where exists (select *from
(select uname from users group by uname having count(*)>1)as s
where u.uname=s.uname )
and u.uid not in(select min(uid)from users group by uname )
delete from users where uid not in(
select uid from users a where not exists (
select 1 from users b where a.uname = b.uname and b.uid < a.uid
));
删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete u from users u where exists(select *from
(select uname,uemail from users group by uname,uemail having count(*)>1)as s
where u.uname=s.uname and u.uemail=s.uemail)
and u.uid not in(select min(uid)from users group by uname,uemail)