sql语句 查重名 ?

xili 2005-07-26 11:57:37
表 ttt( name varchar(20), addr varchar(20))

其中 name有重复数据.

怎么查出 name 重复的 内容. 没有重复的name不需要出现到结果里.
...全文
560 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
你的表只有这两个字段?
xili 2005-07-27
  • 打赏
  • 举报
回复
如果要建临时表,
我宁愿在 c程序里来 计算了.
那样也只要遍历一次就好了.
xili 2005-07-27
  • 打赏
  • 举报
回复
表数据很大,建临时表 不妥吧.

而且 identity字段只能用于 sql server.

我现在只有oracle的环境.

求一个 可移植的,通用的方法.
xili 2005-07-27
  • 打赏
  • 举报
回复
试了一下,如果去掉 and t.addr<>A.addr 就不称哦
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
to filebat(Mark):
昨天就升星了:)
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
--创建测试环境
create table ttt
(
name varchar(20),
addr varchar(20)
)
insert ttt
select '张三','中国北京市' union all
select '李四','中国上海市' union all
select '王五','中国天津市' union all
select '张三','中国四川省'

--测试
declare @tb table(ID int identity,name varchar(20),addr varchar(20))
insert @tb(name,addr) select * from ttt
select name,addr from @tb t
where exists(select 1 from @tb where name=t.name and ID<>t.ID)


--删除测试环境
drop table ttt

--结果
/*
name addr
-------------------- --------------------
张三 中国北京市
张三 中国四川省

(2 row(s) affected)
*/
xili 2005-07-27
  • 打赏
  • 举报
回复
vivian反应真快

addr 可以相同的呀. 还能用这方法吗?
filebat 2005-07-27
  • 打赏
  • 举报
回复
vivianfdlpw, 你这么快就上星了.
PFPF啊.
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
--或者:
select t.* from ttt t
join ttt A on t.name=A.name and t.addr<>A.addr
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
--创建测试环境
create table ttt
(
name varchar(20),
addr varchar(20)
)
insert ttt
select '张三','中国北京市' union all
select '李四','中国上海市' union all
select '王五','中国天津市' union all
select '张三','中国四川省'

--测试
select * from ttt t
where exists(select 1 from ttt where name=t.name and addr<>t.addr)


--删除测试环境
drop table ttt

--结果
/*
name addr
-------------------- --------------------
张三 中国北京市
张三 中国四川省

(2 row(s) affected)
*/
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复 1
select * from ttt t
where exists(seelct 1 from ttt where name=t.name and addr<>t.addr)
xili 2005-07-27
  • 打赏
  • 举报
回复
感谢各位,这个坛子气氛不错.
决定以后也多使用ms的产品.
hanshufan 2005-07-27
  • 打赏
  • 举报
回复 1
select * from ttt t
where
(select count(1) from ttt where name=t.name)>1
order by name
好办法,呵呵学习!
MorningTea 2005-07-27
  • 打赏
  • 举报
回复
filebat(Mark)比较后知后觉,我天天上,昨天就发现 vivianfdlpw() 后面的东西好像少了,呵呵
原来他升星了,可惜不熟,否则一定问候一句,呵呵

问题都解决了,我也发一个,不过没有没有那么简便,惭愧:
select * from ttt t
where exists (select top b.name from (select top 2 t1.name from ttt t1
where name=t1.name order by t1.name) b
where b.name = t.name)
order by name
云中客 2005-07-27
  • 打赏
  • 举报
回复
来晚了,好像问题已经解决了,项一下吧
GRLD8888 2005-07-27
  • 打赏
  • 举报
回复
表 ttt( name varchar(20), addr varchar(20))

其中 name有重复数据.
select * from ttt where name in (select name from ttt group by name having count(name)>1)
vivianfdlpw 2005-07-27
  • 打赏
  • 举报
回复
--创建测试环境
create table ttt
(
name varchar(20),
addr varchar(20)
)
insert ttt
select '张三','中国北京市' union all
select '李四','中国上海市' union all
select '王五','中国天津市' union all
select '张三','中国北京市' union all
select '李四','中国深圳市'

--测试
select * from ttt t
where
(select count(1) from ttt where name=t.name)>1
order by name

--删除测试环境
drop table ttt

--结果
/*
name addr
-------------------- --------------------
李四 中国上海市
李四 中国深圳市
张三 中国北京市
张三 中国北京市

(4 row(s) affected)
*/

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧