Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB cartesian product on same table

I have a one column table with three rows as below:

col1

team1
team2
team3
team4

I want to do a self cartesian join with result as below:
team1, team2
team1, team3
team1, team4
team2, team3
team2, team4
team3, team4

like image 787
NullPointer Avatar asked Mar 15 '23 04:03

NullPointer


1 Answers

cartesian product is cross join in DB terms, you can remove rows where teams are equal in where clause:

select
    t1.col1, t2.col1
from teams as t1
    cross join teams as t2
where
    t1.col1 <> t2.col1
like image 126
Roman Pekar Avatar answered Mar 30 '23 06:03

Roman Pekar