sql 中交叉连接的组合(不是排列)
如果我有一个表想要交叉连接到自身,如何删除重复的行?或者换句话说,我怎样才能进行“顺序无关紧要”的交叉连接?
例如,如果我有一个表 T:
field |
-------
A |
B |
C |
并且我交叉连接到自身,这样我就不会得到 A | A 行
T as t1
cross join
T as t2
on t1.field != t2.field
我会得到以下内容:
field | field
------+-------
A | B
A | C
B | A
B | C
C | A
C | B
但是,对我来说 A,B 与 B,A 相同。
有没有好的方法来删除这些重复项?换句话说,我想要组合而不是排列。
If I have a table that I'd like to cross join to itself, how can I remove the duplicate rows? Or to put it another way, how can I do a "order doesn't matter" cross join?
So for example, if I have a table T:
field |
-------
A |
B |
C |
and I cross join to itself so that i don't get the A | A rows
T as t1
cross join
T as t2
on t1.field != t2.field
I would get the following:
field | field
------+-------
A | B
A | C
B | A
B | C
C | A
C | B
However, to me A, B is the same as B, A.
Is there a good way to remove these duplicates? In other words, I want the combinations not the permutations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FWIW,您可以为此使用
INNER JOIN
,它并不是严格意义上的CROSS JOIN
。 MySQL(也许还有其他一些 RDBMS)将这两种类型的联接视为相同,但在 ANSI SQL 中,交叉联接没有联接条件——它是故意的笛卡尔积。FWIW, you can just use
INNER JOIN
for this, it's not strictly aCROSS JOIN
. MySQL (and perhaps some other RDBMS) treats these two types of join as identical, but in ANSI SQL, a cross join has no join condition -- it's a deliberate Cartesian product.