查询两字段值不相同

sjc8w8e 2012-01-30 04:49:07
有一个表TA两个字段A和B有几组数据如下

A B
1111 1111
1111 AAAA
1111 1111
2222 2222
2222 2222
2222 2222
3333 3333
444 444
找出1111这组数据,因为第二行的B字段和A字段内容不相同
...全文
121 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2012-01-30
  • 打赏
  • 举报
回复

declare @tablename table (A varchar(4),B varchar(4))
insert into @tablename
select '1111','1111' union all
select '1111','AAAA' union all
select '1111','1111' union all
select '2222','2222' union all
select '2222','2222' union all
select '2222','2222' union all
select '3333','3333' union all
select '444','444' union all
select '5555','bbb' union all
select '5555','bbb'

select a.* from @tablename a right join @tablename b
on a.A=b.A left join
(select count(distinct B) as c1,A from @tablename group by A ) c
on b.A=c.A where c.c1>1 and b.A<>b.B

/*
A B
---- ----
1111 1111
1111 AAAA
1111 1111
*/
sjc8w8e 2012-01-30
  • 打赏
  • 举报
回复
感谢各位的回复,这个问题补充一下
A B
1111 1111
1111 AAAA
1111 1111
2222 2222
2222 2222
2222 2222
3333 3333
444 444
5555 bbb
5555 bbb
1111和5555这两组数据A字段的内容和B字段的内容都有不同,但5555组数据的B字段内容是相同的,1111组数据B字段内容不相同,我想要的结果是1111组数据不要5555组数据。非常感谢
Vidor 2012-01-30
  • 打赏
  • 举报
回复
select * from tb t where exists (select 1 from tb where A=t.A and B<>t.B)
Rotel-刘志东 2012-01-30
  • 打赏
  • 举报
回复
----创建测试数据
if object_id('ta') is not null drop table ta
create table ta (A varchar(20),B varchar(20))
insert into ta
select '1111','1111' union all
select '1111','AAAA' union all
select '1111','1111' union all
select '2222','2222' union all
select '2222','2222' union all
select '2222','2222' union all
select '3333','3333' union all
select '444','444'
---查询
(select a ,b from ta)
union
(select a,b from ta)

--结果
/*
a b
1111 1111
1111 AAAA
2222 2222
3333 3333
444 444
*/
Rotel-刘志东 2012-01-30
  • 打赏
  • 举报
回复
----创建测试数据
if object_id('ta') is not null drop table ta
create table ta (A varchar(20),B varchar(20))
insert into ta
select '1111','1111' union all
select '1111','AAAA' union all
select '1111','1111' union all
select '2222','2222' union all
select '2222','2222' union all
select '2222','2222' union all
select '3333','3333' union all
select '444','444'
---查询
(select a ,b from ta)
union
(select a,b from ta)
Rotel-刘志东 2012-01-30
  • 打赏
  • 举报
回复
select * from 
ta where a!=b
叶子 2012-01-30
  • 打赏
  • 举报
回复

declare @tablename table (A varchar(4),B varchar(4))
insert into @tablename
select '1111','1111' union all
select '1111','AAAA' union all
select '1111','1111' union all
select '2222','2222' union all
select '2222','2222' union all
select '2222','2222' union all
select '3333','3333' union all
select '444','444'

select a.* from @tablename a right join @tablename b
on a.A=b.A where b.A<>b.B
/*
A B
---- ----
1111 1111
1111 AAAA
1111 1111
*/
sjc8w8e 2012-01-30
  • 打赏
  • 举报
回复
1111这组其它两行数据就得不到了!
sjc8w8e 2012-01-30
  • 打赏
  • 举报
回复
如果这样写的话我只查到了这一行
1111 AAAA
数据,1111这组数据就得不到呀
紫竹林畔 2012-01-30
  • 打赏
  • 举报
回复
--??
Select *
from #t
where [a]!=[B]

34,591

社区成员

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

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