22,293
社区成员
发帖
与我相关
我的任务
分享
select * from tb a where (select count(*) from tb where left(col,6)=left(a.col,6) )>=2
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] int)
insert [tb]
select 192189101 union all
select 192190056 union all
select 192193580 union all
select 192193644 union all
select 192193722 union all
select 192193833 union all
select 193873699
---查询---
select *
from [tb] t
where (select count(1) from tb where left(col,6)=left(t.col,6))>1
---结果---
col
-----------
192193580
192193644
192193722
192193833
(所影响的行数为 4 行)
select * from 表 where 字段 in(select 字段 from 表 group by left(字段,6) having count(*)>1)
select * from [Table] a where (select count(1) from [Table] where left([Field],6)=left(a.[Field],6))>1
declare @t table(code varchar(10))
insert into @t select '192189101'
insert into @t select '192190056'
insert into @t select '192193580'
insert into @t select '192193644'
insert into @t select '192193722'
insert into @t select '192193833'
insert into @t select '193873699'
select * from @t where left(code,6) in(select left(code,6) from @t group by left(code,6) having count(*)>1)
/*
code
----------
192193580
192193644
192193722
192193833
*/
select * from @t t where exists(select 1 from @t where left(code,6)=left(t.code,6) and code!=t.code)
/*
code
----------
192193580
192193644
192193722
192193833
*/
select * from 表 where left(字段,6) in(select left(字段,6) from 表 group by left(字段,6) having count(*)>1)
/*code
----------
192193580
192193644
192193722
192193833
(4 row(s) affected)*/
declare @t table(code varchar(10))
insert into @t select '192189101'
insert into @t select '192190056'
insert into @t select '192193580'
insert into @t select '192193644'
insert into @t select '192193722'
insert into @t select '192193833'
insert into @t select '193873699'
select * from @t a
where (select count(1) from @t where code like left(a.code,6)+'%') >1
/*
code
----------
192193580
192193644
192193722
*/
select * from tb where left(col,6) in (select left(col,6) from tb group by left(col,6) having count(1) > 1)