select * from book b where exists( select 1 from book where name = b.name and id <> b.id )
select [id], [name] from book order by [name],[id]
select name,count(*) form table group by name having count(name)>1 这样可以吗?
--> 测试数据: #book if object_id('tempdb.dbo.#book') is not null drop table #book create table #book (id int,name varchar(8)) insert into #book select 1,'爆笑强人' union all select 2,'恶搞贴图' union all select 3,'日韩影视' union all select 4,'动物幽默' union all select 5,'华语电视' union all select 6,'恶搞贴图' union all select 7,'爆笑强人' union all select 8,'日韩影视' union all select 9,'言情' union all select 10,'日韩影视'select * from #book as t where exists (select 1 from #book where name=t.name and id<>t.id) -- order by name --> 10W条,不要order by了 /* id name ----------- -------- 1 爆笑强人 2 恶搞贴图 3 日韩影视 6 恶搞贴图 7 爆笑强人 8 日韩影视 10 日韩影视 */
select name,count(*) form table group by name having count(name)>1 这样可以吗? --------------------------------------- 可作为子查询 select * from table where name in (select name from table group by name having count(name)>1)
if object_id('tempdb.dbo.#book') is not null drop table #book create table #book (id int,name varchar(8)) insert into #book select 1,'爆笑强人' union all select 2,'恶搞贴图' union all select 3,'日韩影视' union all select 4,'动物幽默' union all select 5,'华语电视' union all select 6,'恶搞贴图' union all select 7,'爆笑强人' union all select 8,'日韩影视' union all select 9,'言情' union all select 10,'日韩影视' SELECT * FROM #book a WHERE exists(select 1 from #book where id<>a.id and name=a.name) select * from #book a where exists (select 1 from (select name from #book group by name having count(name)>1) b where a.name=b.name ) select * from #book where name in (select name from #book group by name having count(name)>1)
create table tb(id int,name varchar(8)) insert into tb select 1,'爆笑强人' union all select 2,'恶搞贴图' union all select 3,'日韩影视' union all select 4,'动物幽默' union all select 5,'华语电视' union all select 6,'恶搞贴图' union all select 7,'爆笑强人' union all select 8,'日韩影视' union all select 9,'言情' union all select 10,'日韩影视' go select b.id,a.name from tb b inner join (select distinct name from tb) a on a.name =b.name /* id name ----------- -------- 1 爆笑强人 7 爆笑强人 4 动物幽默 2 恶搞贴图 6 恶搞贴图 5 华语电视 3 日韩影视 10 日韩影视 8 日韩影视 9 言情(10 行受影响) */
create table tb(id int,name varchar(8)) insert into tb select 1,'爆笑强人' union all select 2,'恶搞贴图' union all select 3,'日韩影视' union all select 4,'动物幽默' union all select 5,'华语电视' union all select 6,'恶搞贴图' union all select 7,'爆笑强人' union all select 8,'日韩影视' union all select 9,'言情' union all select 10,'日韩影视' go select id,name from tb where name in (select name from tb group by name having count(id)>1) order by name /* id name ----------- -------- 1 爆笑强人 7 爆笑强人 2 恶搞贴图 6 恶搞贴图 3 日韩影视 8 日韩影视 10 日韩影视(7 行受影响)*/
这样可以吗?
if object_id('tempdb.dbo.#book') is not null drop table #book
create table #book (id int,name varchar(8))
insert into #book
select 1,'爆笑强人' union all
select 2,'恶搞贴图' union all
select 3,'日韩影视' union all
select 4,'动物幽默' union all
select 5,'华语电视' union all
select 6,'恶搞贴图' union all
select 7,'爆笑强人' union all
select 8,'日韩影视' union all
select 9,'言情' union all
select 10,'日韩影视'select * from #book as t where exists (select 1 from #book where name=t.name and id<>t.id) -- order by name --> 10W条,不要order by了
/*
id name
----------- --------
1 爆笑强人
2 恶搞贴图
3 日韩影视
6 恶搞贴图
7 爆笑强人
8 日韩影视
10 日韩影视
*/
这样可以吗?
---------------------------------------
可作为子查询
select * from table where name in (select name from table group by name having count(name)>1)
create table #book (id int,name varchar(8))
insert into #book
select 1,'爆笑强人' union all
select 2,'恶搞贴图' union all
select 3,'日韩影视' union all
select 4,'动物幽默' union all
select 5,'华语电视' union all
select 6,'恶搞贴图' union all
select 7,'爆笑强人' union all
select 8,'日韩影视' union all
select 9,'言情' union all
select 10,'日韩影视'
SELECT * FROM #book a WHERE exists(select 1 from #book where id<>a.id and name=a.name) select * from #book a where exists (select 1 from (select name from #book group by name having count(name)>1) b where a.name=b.name ) select * from #book where name in (select name from #book group by name having count(name)>1)
insert into tb
select 1,'爆笑强人' union all
select 2,'恶搞贴图' union all
select 3,'日韩影视' union all
select 4,'动物幽默' union all
select 5,'华语电视' union all
select 6,'恶搞贴图' union all
select 7,'爆笑强人' union all
select 8,'日韩影视' union all
select 9,'言情' union all
select 10,'日韩影视'
go
select b.id,a.name from tb b inner join (select distinct name from tb) a on a.name =b.name
/*
id name
----------- --------
1 爆笑强人
7 爆笑强人
4 动物幽默
2 恶搞贴图
6 恶搞贴图
5 华语电视
3 日韩影视
10 日韩影视
8 日韩影视
9 言情(10 行受影响)
*/
insert into tb
select 1,'爆笑强人' union all
select 2,'恶搞贴图' union all
select 3,'日韩影视' union all
select 4,'动物幽默' union all
select 5,'华语电视' union all
select 6,'恶搞贴图' union all
select 7,'爆笑强人' union all
select 8,'日韩影视' union all
select 9,'言情' union all
select 10,'日韩影视'
go
select id,name from tb where name in (select name from tb group by name having count(id)>1) order by name
/*
id name
----------- --------
1 爆笑强人
7 爆笑强人
2 恶搞贴图
6 恶搞贴图
3 日韩影视
8 日韩影视
10 日韩影视(7 行受影响)*/