--创建测试环境 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) */
--或者: select t.* from ttt t join ttt A on t.name=A.name and t.addr<>A.addr
vivianfdlpw, 你这么快就上星了. PFPF啊.
vivian反应真快addr 可以相同的呀. 还能用这方法吗?
--创建测试环境 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) */
--创建测试环境 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) */
表 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)
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
select * from ttt t where (select count(1) from ttt where name=t.name)>1 order by name 好办法,呵呵学习!
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)
*/
select t.* from ttt t
join ttt A on t.name=A.name and t.addr<>A.addr
PFPF啊.
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)
*/
昨天就升星了:)
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)
*/
select * from ttt where name in (select name from ttt group by name having count(name)>1)
原来他升星了,可惜不熟,否则一定问候一句,呵呵 问题都解决了,我也发一个,不过没有没有那么简便,惭愧:
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
where
(select count(1) from ttt where name=t.name)>1
order by name
好办法,呵呵学习!