加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 没有重复组合的交叉连接

发布时间:2020-12-12 06:29:15 所属栏目:MsSql教程 来源:网络整理
导读:我知道这个问题与这个问题非常相似: Symmetric cross join 而且这个也是: combinations (not permutations) from cross join in sql 但是,如果我们有两个不同的表,比如说A和B: select A.id,B.id from A cross join B 我想考虑(a,b)对(b,a)? 解决方法 sele
我知道这个问题与这个问题非常相似:
Symmetric cross join
而且这个也是:
combinations (not permutations) from cross join in sql

但是,如果我们有两个不同的表,比如说A和B:

select A.id,B.id from A cross join B

我想考虑(a,b)对(b,a)?

解决方法

select A.id aid,B.id bid
from A inner join B on a.id <= b.id
union
select B.id,A.id
from A inner join B on b.id < a.id

如果你想变得更复杂:

select distinct
       case when a.id<=b.id then a.id else b.id end id1,case when a.id<=b.id then b.id else a.id end id2
from A cross join B

在我用小桌子烘烤的小不科学中,后者更快.在下面,将表达式表达式写为子查询.

select distinct
       (select MIN(id) from (select a.id union select b.id)[ ]) id1,(select MAX(id) from (select a.id union select b.id)[ ]) id2
from A cross join B

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读