22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,name nvarchar(30))
insert into tb select 1,'农工商超市有限公司'
insert into tb select 2,'农工商超市有限公司第十一分店'
insert into tb select 3,'农工商超市有限公司第二十分店'
insert into tb select 5,'上海鸿运来大药房有限公司下沙店一分店'
insert into tb select 6,'上海鸿运来大药房有限公司下沙店二分店'
insert into tb select 7,'上海雷允上南翔医药有限公司金耀路药房'
insert into tb select 8,'蒙山县威康医药有限公司'
insert into tb select 9,'孟州市盛兴大药房连锁有限责任公司'
insert into tb select 10,'农工商超市有限公司第十二分店'
go
select * from tb a where exists(select 1 from tb where id<>a.id and left(name,4)=left(a.name,4))
go
drop table tb
/*
id name
----------- ------------------------------
1 农工商超市有限公司
2 农工商超市有限公司第十一分店
3 农工商超市有限公司第二十分店
5 上海鸿运来大药房有限公司下沙店一分店
6 上海鸿运来大药房有限公司下沙店二分店
10 农工商超市有限公司第十二分店
(6 行受影响)
*/
select *
from tb
where left(name,4) in (select distinct left(name,4) from tb group by left(name,4) having count(*) > 1)
--前面有一部分一样的是怎么个一样发,比如前几个,下边是前六个,未测试
select *
from tb
where name in (select name from tb group by left(name,6) having count(*) > 1)