尝试使用存储过程制作乒乓统计跟踪数据库

Trying to make a pingpong stat tracking database with a stored procedure

我正在使用存储过程(尝试)写入 MYsql 中的 3 个不同的 table 以跟踪乒乓数据并显示很酷的统计数据。

所以我完全是 MySQL(和 Whosebug)的菜鸟,之前没有真正使用过任何类型的数据库语言,所以所有这些对我来说都是全新的。我正在尝试创建一个存储过程来写入来自 Ignition 的乒乓统计数据(我很确定 Ignition 不是问题。它告诉我写入失败所以我认为这是我的存储过程的问题) .

我目前有一个存储过程可以写入玩家 table 并且可以在按下按钮时添加赢、输和玩的总游戏数。我现在的问题是我想添加统计数据,以便我可以跟踪比分和谁与谁比赛,这样我就可以制作图表和其他东西。

这个存储过程应该通过 pingpong table 来查找传递的名称之前是否对战过,这样我就可以找到相应的 MatchID。如果玩家之前没有玩过,那么它应该用新的 MatchID 创建一个新行(这是关键,所以每次都应该是唯一的)。有了 MatchID 之后,我就可以计算出玩家之前对战过多少场比赛,比分是多少,谁打败了谁等等。

这是我写的,MySQL 说它很好,但显然它不起作用。我知道它还没有完全完成,但我真的需要一些指导,因为这是我第二次使用 MySQL 或数据库语言做任何事情,我认为当我测试任何类型的写入时这不应该失败.

CREATE DEFINER=`root`@`localhost` PROCEDURE `Matchups`(
#these are passed from Ignition and should be working
IN L1Name VARCHAR(255), #Player 1 name on the left side
IN L2Name VARCHAR(255), #Player 2 name on the left side
IN R1Name VARCHAR(255), #Player 3 name on the right side
IN R2Name VARCHAR(255), #Player 4 name on the right side
IN TWOvTWO int, #If this is 1, then L1,L2,R1,R2 are playing instead of L1,R1
IN LeftScore int,
IN RightScore int)

BEGIN
DECLARE x int DEFAULT 0;
IF((
SELECT MatchupID
FROM pingpong
WHERE (PlayerL1 = L1Name AND PlayerR1 = R1Name) OR (PlayerL1 = R1Name AND PlayerR1 = L1Name)
) 
IS NULL) THEN 
    INSERT INTO pingpong (PlayerL1, PlayerL2, PlayerR1, PlayerR2) VALUES (L1Name, L2Name, R1Name, R2Name);
    INSERT INTO pingponggames (MatchupID, Lscore, Rscore) VALUES ((SELECT MatchupID
    FROM pingpong
    WHERE (PlayerL1 = L1Name AND PlayerR1 = R1Name) OR (PlayerL1 = R1Name AND PlayerR1 = L1Name)), LeftScore, RightScore);
END IF;
END

这是我的 table 目前的样子:

pingpong
PlayerL1 | PlayerL2 | PlayerR1 | PlayerR2 | MatchupID
-----------------------------------------------------
L1       | NULL     | R1       | NULL     | 1
L1       | NULL     | L2       | NULL     | 3
L1       | NULL     | R2       | NULL     | 4
L1       | NULL     | test2    | NULL     | 5
pingponggames
GameID   | MatchupID | LScore   | RScore 
------------------------------------------
1        | 1         | NULL     | NULL     
pingpongplayers
Name     | TotalWins | TotalLosses | GamesPlayed 
-----------------------------------------------------
L1       | 8         | 5           | NULL     
L2       | 1         | 1           | NULL     
R1       | 1         | 6           | 7     
R2       | 1         | 1           | NULL     
test2    | 1         | 0           | 1     
test1    | 0         | 0           | 0

解释了一些功能,如果需要更多,我需要更多信息

CREATE DEFINER=`root`@`localhost` PROCEDURE `Matchups`(
#these are passed from Ignition and should be working
IN L1Name VARCHAR(255), #Player 1 name on the left side
IN L2Name VARCHAR(255), #Player 2 name on the left side
IN R1Name VARCHAR(255), #Player 3 name on the right side
IN R2Name VARCHAR(255), #Player 4 name on the right side
-- what will be the INPUT other than 1? It's to notice doubles or singles right? so taking 0 as single & 1 as doubles
IN TWOvTWO INT, #If this is 1, then L1,L2,R1,R2 are playing instead of L1,R1 
IN LeftScore INT,
IN RightScore INT)

BEGIN
DECLARE x INT DEFAULT 0; # i guess you are using it in the sp 
DECLARE v_matchupid INT; #used int --if data type is different, set as MatchupID column datatype
DECLARE inserted_matchupid INT; -- use data type based on your column MatchupID from pingpong tbl

IF(TWOvTWO=0) THEN -- for singles
#what is the need of this query? to check singles or doubles? Currently it search for only single from what you have written, will change according to that 
SELECT MatchupID INTO v_matchupid
FROM pingpong
WHERE L1Name IN (PlayerL1, PlayerR1) AND R1Name IN (PlayerL1, PlayerR1); # avoid using direct name(string) have a master tbl for player name and use its id to compare or use to refer in another tbl
# the if part checks is it new between them and insert in both tbls
    IF(v_matchupid IS NULL) THEN 
        INSERT INTO pingpong (PlayerL1, PlayerR1) VALUES (L1Name, R1Name);
        SET inserted_matchupid=LAST_INSERT_ID();
        INSERT INTO pingponggames (MatchupID, Lscore, Rscore) VALUES (inserted_matchupid, LeftScore, RightScore);
    /*
    Once I have the MatchID, I can then figure out how many games the players have played against each other before 
    A: this will not work for new matchup since matchupid is created now
    */
    # so assuming if match found update pingponggames tbl with matched matchupid.. i leave it up to you 
    ELSE
        UPDATE pingponggames SET Lscore=LeftScore, Rscore=RightScore WHERE MatchupID=v_matchupid;-- you can write your own 
    END IF;
-- for doubles
ELSE # assuming the possibilities of TWOvTWO will be either 0 or 1 if more use "elseif(TWOvTWO=1)" for this block as doubles
    SELECT MatchupID INTO v_matchupid
    FROM pingpong
    # Note: If player name are same it will be difficult so better use a unique id as reference
    WHERE   L1Name IN (PlayerL1, PlayerL2, PlayerR1, PlayerR2) AND
            L2Name IN (PlayerL1, PlayerL2, PlayerR1, PlayerR2) AND
            R1Name IN (PlayerL1, PlayerL2, PlayerR1, PlayerR2) AND
            R2Name IN (PlayerL1, PlayerL2, PlayerR1, PlayerR2);

    IF(v_matchupid IS NULL) THEN
        INSERT INTO pingpong (PlayerL1, PlayerL2, PlayerR1, PlayerR2) VALUES (L1Name, L2Name, R1Name, R2Name);
        SET inserted_matchupid=LAST_INSERT_ID();
        INSERT INTO pingponggames (MatchupID, Lscore, Rscore) VALUES (inserted_matchupid, LeftScore, RightScore);
    ELSE
        UPDATE pingponggames SET Lscore=LeftScore, Rscore=RightScore WHERE MatchupID=v_matchupid;-- you can write your own 
    END IF;

END IF;
END