根据2个玩家数据选择获胜者

时间:2017-11-01 15:52:23

标签: sql oracle

所以我有两张基于下面的表格:

Table : Player 
Code  Name  Address 
-------------------------
001   John   Miami
002   Winda  Washington
003   Ferry  New York
004   Chase  Texas
________________________

Table : Match
No      Date        Player1     Player2     Result
0001    01/11/17    001         003         003
0002    01/11/17    002         004         002
0003    02/11/17    001         002         001
0004    02/11/17    003         004         004
0005    03/11/17    001         004         001
0006    03/11/17    002         003         002

因此,基于这两个表,我想创建一个select查询来生成如下结果:

No      Date        Winner      Loser
0001    01-Nov-17   Ferry       John
0002    01-Nov-17   Winda       Chase 
0003    02-Nov-17   John        Winda
0004    02-Nov-17   Chase       Ferry
0005    03-Nov-17   John        Chase
0006    03-Nov-17   Winda       Ferry

朋友告诉我使用不等于运算符(!=) 以前我已经做过这个

 
select b.No, b.Date, c.Name as "Winner", d.Name as "Loser"
from match b, player c, player d
where b.Result = c.code >> then i got stuck

关于以下问题的任何想法?

非常感谢!

1 个答案:

答案 0 :(得分:1)

使用ANSI Join语法和CASE表达式:

select m.No,
       m."Date",
       CASE m.Result WHEN m.Player1 THEN p1.Name ELSE p2.Name END as Winner,
       CASE m.Result WHEN m.Player1 THEN p2.Name ELSE p1.Name END as Loser
FROM   match m
       INNER JOIN player p1
       ON ( m.Player1 = p1.code )
       INNER JOIN player p2
       ON ( m.Player2 = p2.code )