我努力遏制MySQL代碼創建一個Row_Number列在我的表「starting_pitcher_stats 「我想從1開始,但是從新年伊始重新開始,並且有一個新的投手。我用下面的代碼來創建ROW_NUMBER列:試圖創建一個「行號」列,重新從1開始新的一年,當有新的陳述投手
理想的情況下,該表是這樣的:
Starting_Pitcher park_factor std_PF Row_Number Game_Date Game_Number
aased001 108 108 1 1977-07-26 0
aased001 94 101 2 1977-07-31 0
aased001 100 100.66 3 1977-08-06 0
aased001 108 102.5 4 1977-08-11 0
aased001 108 103.66 5 1977-08-16 0
aased001 96 102.33 6 1977-08-21 0
aased001 108 103.14 7 1977-08-26 0
aased001 108 103.75 8 1977-08-31 0
aased001 104 103.77 9 1977-09-05 1
aased001 108 104.2 10 1977-09-10 0
aased001 92 103.09 11 1977-09-16 0
aased001 106 103.33 12 1977-09-22 0
aased001 108 103.69 13 1977-09-27 1
aased001 96 96 1 1978-04-11 0
aased001 100 13.06 2 1978-04-16 0
aased001 100 18.5 3 1978-04-21 0
aased001 96 23.05 4 1978-04-28 0
...因爲它是現在,該表的樣本如下:
Starting_Pitcher park_factor std_PF Row_Number Game_Date Game_Number
aased001 108 108 1 1977-07-26 0
aased001 94 101 2 1977-07-31 0
aased001 100 100.66 3 1977-08-06 0
aased001 108 102.5 4 1977-08-11 0
aased001 108 103.66 5 1977-08-16 0
aased001 96 102.33 6 1977-08-21 0
aased001 108 103.14 7 1977-08-26 0
aased001 108 103.75 8 1977-08-31 0
aased001 104 103.77 9 1977-09-05 1
aased001 108 104.2 10 1977-09-10 0
aased001 92 103.09 11 1977-09-16 0
aased001 106 103.33 12 1977-09-22 0
aased001 108 103.69 13 1977-09-27 1
aased001 96 96 14 1978-04-11 0
aased001 100 13.06 15 1978-04-16 0
aased001 100 18.5 16 1978-04-21 0
aased001 96 23.05 17 1978-04-28 0
,我用下面的代碼來創建它:當我使用下面的代碼
ALTER TABLE starting_pitcher_stats ADD Row_Number int(11) DEFAULT '0' NOT NULL;
SELECT @n:=0, Row_Number, Starting_Pitcher, lg_ID, YEAR_ID, Game_Date, Game_Number FROM starting_pitcher_stats;
UPDATE starting_pitcher_stats SET Row_Number = @n := @n + 1
使ROW_NUMBER列重啓1在新的一年的開始,當有一個新的投手,這是行不通的:
ALTER TABLE starting_pitcher_stats ADD ROW_NUMBER1 int(11) DEFAULT '0' NOT NULL;
SELECT @n:=0, Row_Number, Starting_Pitcher, lg_ID, YEAR_ID, Game_Date, Game_Number FROM starting_pitcher_stats;
UPDATE starting_pitcher_stats IF std_PF=park_factor THEN SET Row_Number=1 ELSE SET Row_Number1 = @n := @n + 1
我得到以下錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF std_PF=park_factor THEN SET Row_Number=1 ELSE SET Row_Number' at line 1
是有可能將這個Row_Number列設置爲從1開始,即使我重新排序(或將其分組)到另一列(如Game_Date列)時也是如此。
有人可以幫助嗎?
預先感謝您。 李
UPDATE:戈登,這裏是我的錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1, 1)
)
) as rn
from starting_p' at line 4
我敢肯定所需的編輯是顯而易見的,但我的經驗是不夠的,能夠發現它。
是的,有一個唯一的ID 「GAME_ID」 封裝 「YEAR_ID」, 「GAME_DATE」, 「Game_Number」,和主隊的那場比賽 「Park_ID」。感謝提醒我關於double_headers ...「Game_Number」是我從可用的「GAME_ID」字段中派生出來的字段,如果有的話,它指的是雙頁眉的遊戲「1」或遊戲「2」或者遊戲「0」,如果一場比賽。這聽起來像使用GAME_ID會使過程更有效率,而不是單獨加入Year_ID,Game_Date和Game Number?
下面是表的樣本的屏幕截圖,這次包括GAME_ID和YEAR_ID列:
我還是想知道,所有的代碼...沒有的「@sy」中的「sy」必須在某處進行定義?
我 非常感謝您的幫助。 李
UPDATE: 這裏的時候我想,我已經通過刪除括號和改變某些字段的名稱編輯以我的表匹配的代碼,我收到的錯誤:
Incorrect integer value: 'aased001:1977:1:1' for column 'row_number' at row 1
這裏有最新的我使用的代碼產生了上述錯誤。我希望我沒有在不經意間把代碼還比它應該完成遠:
UPDATE starting_pitcher_stats JOIN
(select starting_pitcher_stats.*,
(@rn := if(@sy = concat_ws(':', Starting_Pitcher, YEAR_ID), @rn + 1,
@sy := concat_ws(':', Starting_Pitcher, YEAR_ID, 1, 1)
)
) as rn
from starting_pitcher_stats
cross join
(select @rn := 0, @sy := '') AS params
order by Starting_Pitcher, YEAR_ID, Game_Date, Game_Number
) as b
on b.Starting_Pitcher = starting_pitcher_stats.Starting_Pitcher AND
b.YEAR_ID = starting_pitcher_stats.YEAR_ID AND
b.Game_Date = starting_pitcher_stats.Game_Date AND
b.Game_Number=starting_pitcher_stats.Game_Number
set starting_pitcher_stats.row_number= b.rn
更新:這裏是沒有錯誤的工作代碼:
UPDATE starting_pitcher_stats JOIN
(select starting_pitcher_stats.*,
(@rn := if(@sy = concat_ws(':', Starting_Pitcher, YEAR_ID), @rn + 1,
if(@sy := concat_ws(':', Starting_Pitcher, YEAR_ID), 1, 1)
)
) as rn
from starting_pitcher_stats CROSS JOIN
(select @rn := 0, @sy := '') params
order by Starting_Pitcher, YEAR_ID, Game_Date, Game_Number
) sp2
on sp2.Starting_Pitcher = starting_pitcher_stats.Starting_Pitcher AND
sp2.YEAR_ID = starting_pitcher_stats.YEAR_ID AND
sp2.Game_Date = starting_pitcher_stats.Game_Date AND
sp2.Game_Number=starting_pitcher_stats.Game_Number
set starting_pitcher_stats.row_number = sp2.rn;
使用插入觸發器?在插入選擇給定的開始投手和給定年份的所有行。如果結果爲空,則插入1,否則插入最大行數+1。 – ShuberFu
糟糕,誤解。任何方式,仍嘗試插入觸發器。但是,只需選擇最後一行。如果年份和投手不同,請插入1.如果它們相同,則插入上述行號+ 1. – ShuberFu