shushu304
shushu304

Reputation: 1498

select all available combination by specific field in one query

I have table "teams" with fields: id, team_name

  id, team_name

i want to select in one query all available VS combinations ...

like that:

Real Madrid - FC Barcelona

Real Madrid - juventus

Real Madrid - Milan

FC Barcelona - juventus

FC Barcelona - Milan

juventus - Milan

the purpose is to find solution for dynamic with different amount of rows if will be more teams in the tables...

this is possible? how... ?

Thanks :)

Upvotes: 0

Views: 58

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

Use a self join.

select t1.team_name,t2.team_name
from tbl t1
join tbl t2 on t1.team_name>t2.team_name --or if the id's are unique use 
                                          /* on t1.id < t2.id */

Upvotes: 5

Related Questions