sql语句如何实现同一张表里交叉相等的记录并排显示出来?
比如有一张表
字段 A B
苹果 梨
香蕉 桃子
香蕉 西瓜
苹果 水果
梨 苹果
西瓜 香蕉
我想实现的是找出A、B两字段交叉相等的记录,并将交叉相等的两记录相邻显示出来,如下所示:
A B
苹果 梨
梨 苹果
香蕉 西瓜
西瓜 香蕉
该怎么写sql语句?
[解决办法]
这个错了...用下面的..
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
[解决办法]
row_number是sql2005+才有的