回答編集履歴

3

goal_timeを日時と勘違いした時の間違いが残っていた

2021/03/04 15:00

投稿

Akiya
Akiya

スコア144

test CHANGED
@@ -84,54 +84,72 @@
84
84
 
85
85
  SELECT
86
86
 
87
- (
87
+ (
88
88
 
89
- SELECT
89
+ SELECT
90
90
 
91
- COUNT(1) + 1
91
+ COUNT(1) + 1
92
92
 
93
- FROM
93
+ FROM
94
94
 
95
- goals g
95
+ goals g
96
96
 
97
- WHERE
97
+ INNER JOIN pairings p
98
98
 
99
- goals.player_id = g.player_id
99
+ ON g.pairing_id = p.id
100
100
 
101
- AND goals.goal_time > g.goal_time
101
+ WHERE
102
102
 
103
- ) AS 点数
103
+ goals.player_id = g.player_id
104
104
 
105
- , pairings.kickoff AS 試合日時
105
+ AND (
106
106
 
107
- , countries.name AS 対戦相手
107
+ (
108
108
 
109
+ goals.goal_time > g.goal_time
110
+
111
+ AND pairings.kickoff = p.kickoff
112
+
113
+ ) OR (
114
+
115
+ pairings.kickoff > p.kickoff
116
+
117
+ )
118
+
119
+ )
120
+
121
+ ) AS 点数
122
+
123
+ , pairings.kickoff AS 試合日時
124
+
125
+ , countries.name AS 対戦相手
126
+
109
- , goals.goal_time AS ゴールタイム
127
+ , goals.goal_time AS ゴールタイム
110
128
 
111
129
  FROM
112
130
 
113
- players
131
+ players
114
132
 
115
- INNER JOIN goals
133
+ INNER JOIN goals
116
134
 
117
- ON players.id = goals.player_id
135
+ ON players.id = goals.player_id
118
136
 
119
- INNER JOIN pairings
137
+ INNER JOIN pairings
120
138
 
121
- ON goals.pairing_id = pairings.id
139
+ ON goals.pairing_id = pairings.id
122
140
 
123
- INNER JOIN countries
141
+ INNER JOIN countries
124
142
 
125
- ON pairings.enemy_country_id = countries.id
143
+ ON pairings.enemy_country_id = countries.id
126
144
 
127
145
  WHERE
128
146
 
129
- players.id = 対象プレーヤーのID
147
+ players.id = 対象プレーヤーのID
130
148
 
131
- ORDER BY
149
+ ORDER BY
132
150
 
133
- pairings.kickoff
151
+ pairings.kickoff
134
152
 
135
- , goals.goal_time
153
+ , goals.goal_time
136
154
 
137
155
  ```

2

goal_timeが日時時ではないための修正

2021/03/04 15:00

投稿

Akiya
Akiya

スコア144

test CHANGED
@@ -58,7 +58,9 @@
58
58
 
59
59
  ORDER BY
60
60
 
61
+ tmp.kickoff
62
+
61
- tmp.goal_time
63
+ , tmp.goal_time
62
64
 
63
65
  ```
64
66
 
@@ -128,6 +130,8 @@
128
130
 
129
131
  ORDER BY
130
132
 
133
+ pairings.kickoff
134
+
131
- goals.goal_time
135
+ , goals.goal_time
132
136
 
133
137
  ```

1

他のパターンの追記

2021/03/04 13:47

投稿

Akiya
Akiya

スコア144

test CHANGED
@@ -67,3 +67,67 @@
67
67
  デーブルの関係など詳細が分からないのでもろもろエスパーですが、こんな感じでしょうか。
68
68
 
69
69
  Window関数を使用しているので、MySQL8.0からしか動きません。
70
+
71
+
72
+
73
+ 追記
74
+
75
+ 上記の「点数」は「1試合内の何点目か」を出していました。
76
+
77
+ 「該当選手の生涯で何点目か」であれば、下記のような感じです。
78
+
79
+
80
+
81
+ ```sql
82
+
83
+ SELECT
84
+
85
+ (
86
+
87
+ SELECT
88
+
89
+ COUNT(1) + 1
90
+
91
+ FROM
92
+
93
+ goals g
94
+
95
+ WHERE
96
+
97
+ goals.player_id = g.player_id
98
+
99
+ AND goals.goal_time > g.goal_time
100
+
101
+ ) AS 点数
102
+
103
+ , pairings.kickoff AS 試合日時
104
+
105
+ , countries.name AS 対戦相手
106
+
107
+ , goals.goal_time AS ゴールタイム
108
+
109
+ FROM
110
+
111
+ players
112
+
113
+ INNER JOIN goals
114
+
115
+ ON players.id = goals.player_id
116
+
117
+ INNER JOIN pairings
118
+
119
+ ON goals.pairing_id = pairings.id
120
+
121
+ INNER JOIN countries
122
+
123
+ ON pairings.enemy_country_id = countries.id
124
+
125
+ WHERE
126
+
127
+ players.id = 対象プレーヤーのID
128
+
129
+ ORDER BY
130
+
131
+ goals.goal_time
132
+
133
+ ```