16 个解决方案
#1
有一表tb,表中有一名为“name”的字段,现在我想检索出表中所有name相同的数据,请问大家该怎么做?
===========难道是这样===========
SELECT NAME
FROM TB
GROUP BY NAME
HAVING COUNT(*)>1
===========难道是这样===========
SELECT NAME
FROM TB
GROUP BY NAME
HAVING COUNT(*)>1
#2
select * from table as a
where exists(select 1 from table where name = a.name group by name having count(*) > 1)
where exists(select 1 from table where name = a.name group by name having count(*) > 1)
#3
select * from tb where name in
(
select name from tb group by name having count(*)>1
)
(
select name from tb group by name having count(*)>1
)
#4
谢谢二位,查出来了,那怎么样把查出来的数据只保留一条呢?就是要保证name列中没有重复的,有就删掉
#5
这不更容易啊!!!!
select name from tb group by name
select name from tb group by name
#6
重做,刚刚的没有很好理解题意!
#7
谢谢二位,查出来了,那怎么样把查出来的数据只保留一条呢?就是要保证name列中没有重复的,有就删掉
-------------------------------------------------
select name from t2 group by name having count(*)>1
-------------------------------------------------
select name from t2 group by name having count(*)>1
#8
to :liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不)
你这样做的效果和select distinct name from pb 不是一样吗?只能让name列不重复,但是我删的时候没法删啊
你这样做的效果和select distinct name from pb 不是一样吗?只能让name列不重复,但是我删的时候没法删啊
#9
有主键吗?
说一下表结构吧!
说一下表结构吧!
#10
select name ,count(*) from tb group by name having count(*) > 1
#11
假设表有ID,并且name相同的行ID值不同:
----删除重复行方案A:保留id最大的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,max(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id > t.id)
----删除重复行方案B:保留id最小的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,min(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id < t.id)
----删除重复行方案A:保留id最大的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,max(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id > t.id)
----删除重复行方案B:保留id最小的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,min(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id < t.id)
#12
为描述简单,表tb中只有两个列,一个是ID,一个是name,现在name中有重复的数据,我要把重复的数据的数据删掉,或者取得几行name重复的数据的ID号也行
#13
或者取得几行name重复的数据其中一条的ID号也行
#14
OK,谢谢hellowork(一两清风) ,谢谢大家!
#15
select * into #t4 from ta group by name
delete from ta
insert into ta select * from #t4
drop table #t4
----------------------结果一样,你可能不喜欢
delete from ta
insert into ta select * from #t4
drop table #t4
----------------------结果一样,你可能不喜欢
#16
----------------------结果一样,你可能不喜欢
to liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不) :
---------------------------------------------------------
喜欢喜欢,只要能解决问题我都喜欢,呵呵
重点感谢:liangfei1983,hellowork!
也谢谢大家,30分有点少,加分结贴!
to liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不) :
---------------------------------------------------------
喜欢喜欢,只要能解决问题我都喜欢,呵呵
重点感谢:liangfei1983,hellowork!
也谢谢大家,30分有点少,加分结贴!
#1
有一表tb,表中有一名为“name”的字段,现在我想检索出表中所有name相同的数据,请问大家该怎么做?
===========难道是这样===========
SELECT NAME
FROM TB
GROUP BY NAME
HAVING COUNT(*)>1
===========难道是这样===========
SELECT NAME
FROM TB
GROUP BY NAME
HAVING COUNT(*)>1
#2
select * from table as a
where exists(select 1 from table where name = a.name group by name having count(*) > 1)
where exists(select 1 from table where name = a.name group by name having count(*) > 1)
#3
select * from tb where name in
(
select name from tb group by name having count(*)>1
)
(
select name from tb group by name having count(*)>1
)
#4
谢谢二位,查出来了,那怎么样把查出来的数据只保留一条呢?就是要保证name列中没有重复的,有就删掉
#5
这不更容易啊!!!!
select name from tb group by name
select name from tb group by name
#6
重做,刚刚的没有很好理解题意!
#7
谢谢二位,查出来了,那怎么样把查出来的数据只保留一条呢?就是要保证name列中没有重复的,有就删掉
-------------------------------------------------
select name from t2 group by name having count(*)>1
-------------------------------------------------
select name from t2 group by name having count(*)>1
#8
to :liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不)
你这样做的效果和select distinct name from pb 不是一样吗?只能让name列不重复,但是我删的时候没法删啊
你这样做的效果和select distinct name from pb 不是一样吗?只能让name列不重复,但是我删的时候没法删啊
#9
有主键吗?
说一下表结构吧!
说一下表结构吧!
#10
select name ,count(*) from tb group by name having count(*) > 1
#11
假设表有ID,并且name相同的行ID值不同:
----删除重复行方案A:保留id最大的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,max(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id > t.id)
----删除重复行方案B:保留id最小的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,min(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id < t.id)
----删除重复行方案A:保留id最大的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,max(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id > t.id)
----删除重复行方案B:保留id最小的行,删除其它行
--方法1
delete tb from tb t
inner join(select name,min(id) as id from tb group by name) a
on t.name = a.name and t.id <> a.id
--方法2
delete tb from tb t
where exists(select 1 from tb where name = t.name and id < t.id)
#12
为描述简单,表tb中只有两个列,一个是ID,一个是name,现在name中有重复的数据,我要把重复的数据的数据删掉,或者取得几行name重复的数据的ID号也行
#13
或者取得几行name重复的数据其中一条的ID号也行
#14
OK,谢谢hellowork(一两清风) ,谢谢大家!
#15
select * into #t4 from ta group by name
delete from ta
insert into ta select * from #t4
drop table #t4
----------------------结果一样,你可能不喜欢
delete from ta
insert into ta select * from #t4
drop table #t4
----------------------结果一样,你可能不喜欢
#16
----------------------结果一样,你可能不喜欢
to liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不) :
---------------------------------------------------------
喜欢喜欢,只要能解决问题我都喜欢,呵呵
重点感谢:liangfei1983,hellowork!
也谢谢大家,30分有点少,加分结贴!
to liangfei1983(我是杀人不眨眼,眨眼不杀人的恶魔!哈哈,怕了吧,不) :
---------------------------------------------------------
喜欢喜欢,只要能解决问题我都喜欢,呵呵
重点感谢:liangfei1983,hellowork!
也谢谢大家,30分有点少,加分结贴!