日期:2014-05-17  浏览次数:20407 次

sql语句如何实现同一张表里交叉相等的记录并排显示出来?
比如有一张表
字段 A     B      
     苹果  梨
     香蕉  桃子     
     香蕉  西瓜  
     苹果  水果   
     梨    苹果
     西瓜  香蕉   
我想实现的是找出A、B两字段交叉相等的记录,并将交叉相等的两记录相邻显示出来,如下所示:
    A     B      
    苹果  梨   
    梨    苹果
  
    香蕉  西瓜  
    西瓜  香蕉
  
      
该怎么写sql语句?  

------解决方案--------------------
引用:
SQL code
?



12345678910111213

with tb(a ,b ) as (  select '1','2' union allselect '唉','压' union allselect '2','1' union allselect '5','1' union allselect '3','1' union allselect '4','1' unio……


这个错了...用下面的..

with tb(a ,b ) as ( 
select '1','2' union all
select '唉','压' union all
select '2','1' union all
select '5','1' union all
select '3','1' union all
select '4','1' union all
select '1','3' union all
select '1','5' union all
select '压','唉'
) select a.* from tb a,tb b where a.a=b.b and a.b=b.a
order by ascii(a.b)+ascii(b.b)

------解决方案--------------------
select * from tb as t where exists (select 1 from tb where A=t.B and B=t.A)
order by case A>B when B else A end

------解决方案--------------------
引用:
在sql查询分析器中提示'ROW_NUMBER' 不是可以识别的 函数名。

row_number是sql2005+才有的