首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查询两字段值不相同解决方案

2012-03-24 
查询两字段值不相同有一个表TA两个字段A和B有几组数据如下AB111111111111AAAA111111112222222222222222222

查询两字段值不相同
有一个表TA两个字段A和B有几组数据如下

  A B
  1111 1111
  1111 AAAA
  1111 1111
  2222 2222
  2222 2222
  2222 2222
  3333 3333
  444 444
找出1111这组数据,因为第二行的B字段和A字段内容不相同

[解决办法]

SQL code
--??Select * from #twhere [a]!=[B]
[解决办法]
SQL code
declare @tablename table (A varchar(4),B varchar(4))insert into @tablenameselect '1111','1111' union allselect '1111','AAAA' union allselect '1111','1111' union allselect '2222','2222' union allselect '2222','2222' union allselect '2222','2222' union allselect '3333','3333' union allselect '444','444'select a.* from @tablename a right join @tablename bon a.A=b.A where b.A<>b.B/*A    B---- ----1111 11111111 AAAA1111 1111*/
[解决办法]
SQL code
select * from ta where a!=b
[解决办法]
SQL code
----创建测试数据if object_id('ta')  is not null  drop table tacreate table ta (A varchar(20),B varchar(20))insert into ta select '1111','1111' union allselect '1111','AAAA' union allselect '1111','1111' union allselect '2222','2222' union allselect '2222','2222' union allselect '2222','2222' union allselect '3333','3333' union allselect '444','444'---查询(select a ,b from ta)union (select a,b from ta)
[解决办法]
SQL code
declare @tablename table (A varchar(4),B varchar(4))insert into @tablenameselect '1111','1111' union allselect '1111','AAAA' union allselect '1111','1111' union allselect '2222','2222' union allselect '2222','2222' union allselect '2222','2222' union allselect '3333','3333' union allselect '444','444' union allselect '5555','bbb' union allselect '5555','bbb'select a.* from @tablename a right join @tablename bon a.A=b.A left join(select count(distinct B) as c1,A from @tablename group by A ) con b.A=c.A where c.c1>1 and b.A<>b.B/*A    B---- ----1111 11111111 AAAA1111 1111*/ 

热点排行