|
我最近接手了我在壁球俱乐部的内部联赛# A4 ~/ N6 r# X% b
我希望把这些内容放在网上,让会员查看并添加所需的结果/ v: R: A9 O3 X
联赛结构遵循以下格式,共有6个联赛( R* O; V7 o1 s
联赛1
- R9 K# ~" b6 d( a. d| | John | Mark | Peter | Martin | Paul ||:------:|:----:|:----:|:-----:|:------:|:----:|| John | NULL | 3 | 0 | 1 | 2 || Mark | 0 | NULL | 1 | 3 | 0 || Peter | 3 | 3 | NULL | 1 | 3 || Martin | 3 | 1 | 3 | NULL | 2 || Paul | 3 | 3 | 0 | 3 | NULL |联赛20 Q5 l; W& V$ O( K ]- d
等
$ }$ j+ R: b: ~" e$ x4 x) p5 I+ u我将表结构设计为
1 ?2 L1 {' x t9 }$ [ oCREATE TABLE [dbo].[Results]( [ResultId] [int] IDENTITY(1,1) NOT NULL, [LeagueId] [int] NOT NULL, [Player1Id] [int] NOT NULL, [Player2Id] [int] NOT NULL, [Player1Result] [int] NULL, [Player2Result] [int] NULL)CREATE TABLE [dbo].[Players]( [PlayerId] [int] IDENTITY(1,1) NOT NULL, [UserId] [int] NOT NULL, [FirstName] [nvarchar](150) NULL, [LastName] [nvarchar](150) NULL)CREATE TABLE [dbo].[Leagues]( [LeagueId] [int] IDENTITY(1,1) NOT NULL, [LeagueName] [nvarchar](50) NULL)我试着写一个查询,它为我提供了每个细分的输出,而不是几个查询中的输出,任何人都能帮助查询吗?
) Z, f, @2 f t/ o$ w4 Y到目前为止,我拥有的是我所拥有的
" w S+ V2 G' m5 L ]select p.FirstName,p1.player2result,p2.player2result,p3.player2result,p4.player2resultfrom (select player2Result from Results p1 where p.playerId = p1.Player2Id union select player2Result from Results p2 where p.playerId = p2.Player2Id union select player2Result from Results p3 where p.playerId = p3.Player2Id union select player2Result from Results p4 where p.playerId = p4.Player2Id) as opResultLEFT JOIN Players p on opResult.Player2Result = p.PlayerIdGROUP BY p.FirstName,p1.player2result,p2.player2result,p3.player2result,p4.player2result
; q5 N( ?- S& X) A2 O$ i) G 解决方案: |
|