查询以安排每个团队之间的匹配

时间:2017-09-17 09:31:59

标签: sql oracle

如何使用Oracle中的SQL查询获取以下输出

场景:

假设在名为Team的表中包含4条记录,我们需要安排每个团队与对方团队之间的匹配:

 Team
    ----
    India
    Pakistan
    Srilanka
    Australia


    Output :
    ----
    India VS Pakistan
    India VS Srilanka
    India VS Australia
    Pakistan VS Srilanka
    Pakistan VS Australia
    Srilanka VS Australia

7 个答案:

答案 0 :(得分:3)

您可以使用简单的join执行此操作。 。 。但不是cross join

with teams as (
      select 'India' as team FROM dual UNION ALL
      select 'Pakistan' as team FROM dual UNION ALL
      select 'Srilanka' as team FROM dual UNION ALL
      select 'Australia' as team FROM dual UNION ALL
     )
select t1.team as team1, team2.team team2
from teams t1 join
     teams t2
     on t1.name < t2.name;

这会为每对产生一行。

如果你真的想保留订单,那么你得到的输出就是你指定的对,你需要一个订购栏:

with teams as (
      select 'India' as team, 1 as n FROM dual UNION ALL
      select 'Pakistan' as team, 2 FROM dual UNION ALL
      select 'Srilanka' as team, 3 FROM dual UNION ALL
      select 'Australia' as team, 4 FROM dual UNION ALL
     )
select t1.team as team1, team2.team team2
from teams t1 join
     teams t2
     on t1.n < t2.n;

答案 1 :(得分:2)

您可以在同一个表格之间使用完整联接,其中a.team&lt;&gt; b.team

select 
       a.team_name || 'VS' || b.team_name
from   
table_team a,  
table_team b
where 
       a.team_id < b.team_id ;

如果我们考虑&lt;&gt;然后会有重复,例如。 Ind vs Pak和Pak vs Ind。所以字符串比较少,总是会选择组合

答案 2 :(得分:0)

WITH teams
     AS (SELECT 'India' team
         FROM dual
         UNION ALL
         SELECT 'Pakistan' team
         FROM dual
         UNION ALL
         SELECT 'Srilanka' team
         FROM dual
         UNION ALL
         SELECT 'Australia' team
         FROM dual
         )
SELECT DISTINCT CASE
                  WHEN t1.team >= t2.team THEN t2.team
                  ELSE t1.team
                END  || ' VS '||
                CASE
                  WHEN t1.team >= t2.team THEN t1.team
                  ELSE t2.team
                END Matches
FROM teams t1
       cross join teams t2
WHERE t1.team != t2.team
ORDER BY Matches;

输出:

Australia VS India
Australia VS Pakistan
Australia VS Srilanka
India VS Pakistan
India VS Srilanka
Pakistan VS Srilanka

您可以在CASE

中使用其他组合

答案 3 :(得分:0)

使用WC作为 ( 从双重工会中选择“印度”团队 从双重联盟中选择“巴基斯坦”团队 从双重工会中全部选择“英格兰”队 从双重身份中选择“澳大利亚”团队 ) 选择wc1.team,wc2.team 从WC wc1 交叉连接WC wc2 其中wc1.team!= wc2.team 按wc2.team,wc1.team分组;

答案 4 :(得分:0)

编写如下查询。

SELECT us.user_firstname   AS FirstPlayerFName, 
       us.user_lastname    AS FirstPlayerLName, 
       u.user_firstname    AS SecondPlayerFName, 
       u.user_lastname     AS SecondPlayerLName, 
       tm1.mapped_playerid AS firstPlayerId, 
       tm2.mapped_playerid AS SecondPlayerId 
FROM   tbl_tournamentplayermapping tm1 
       JOIN tbl_tournamentplayermapping tm2 
         ON tm1.mapped_playerid < tm2.mapped_playerid 
       JOIN tbl_user u 
         ON u.user_id = tm1.mapped_playerid 
       JOIN tbl_user us 
         ON us.user_id = tm2.mapped_playerid 
WHERE  tm1.mapped_tournamnetgroup = 'A' 
       AND tm2.mapped_tournamnetgroup = 'A' 
       AND tm1.mapped_scheduleid = tm2.mapped_scheduleid 

答案 5 :(得分:0)

您可以不交叉加入。试试这个。

 create table testData(    
    team varchar(10),
   );

insert into testData values ('India');
insert into testData values ('Pakistan');
insert into testData values ('Srilanka');
insert into testData values ('Australia');


select t1.team + ' VS ' + t2.team
  from testData t1,testData t2
 where t1.team < t2.team
 order by t1.team

答案 6 :(得分:-1)

您可以尝试该查询:

select a.ename "team-a",b.ename "team-b" from #emp a,#emp b where  a.eid != b.eid  order by "team-a"