0

嗨,我想在 mysql 中编写一个存储过程,它返回不同类型的表,但每次只有一行。我无法使用可以检索多个结果集的功能。但如果我可以使用这样的东西会很好:

main:BEGIN
    IF EXISTS(SELECT 1 
                FROM Ban
               WHERE (`Ban`.`Name` = _IP OR `Ban`.`IP` = _IP)
                 AND `Ban`.`Date` > _Date LIMIT 1)
    THEN BEGIN
        SELECT 0 AS LoginType, `Ban`.`RegID`, `Ban`.`Name`, `Ban`.`Reason`,
               `Ban`.`Date`, `Eredmeny`.`Name`, `Ban`.`UnbanDate`, `Ban`.`BanType` 
          FROM `Ban` 
          LEFT OUTER JOIN (SELECT DISTINCT RegID, Name 
                             FROM ((SELECT Players.RegID,Players.Name FROM Players)
                                    UNION ALL
                                   (SELECT PlayersArchive.RegID, PlayersArchive.Name
                                     FROM PlayersArchive)) Eredmeny) AS Eredmeny
                       ON Ban.RegID = Eredmeny.RegID
         WHERE (`Ban`.`Name` = _Name OR `Ban`.`IP` = _IP)
           AND `Ban`.`Date` > _Date LIMIT 1;
        LEAVE main;
        END;
    END IF;

    IF EXISTS(SELECT 1 FROM `Players` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 1 AS LoginType,Players.*
          FROM `Players` WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;

    IF EXISTS(SELECT 1 FROM `PlayersArchive` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 2 AS LoginType,PlayersArchive.*
          FROM `PlayersArchive`
         WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;  

    IF EXISTS(SELECT 1 FROM `PreReg` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 3 AS LoginType,`Name`, `Password`, `Email`, `RegDate`, `Level`,
               `StuntDone`, `DriftDone`, `ParkourDone`, `RolePlayDone`,
               `DeathmatchDone`, `PosX`, `PosY`, `PosZ`, `PosA`
          FROM `PreReg`
         WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;

    SELECT 4 AS LoginType LIMIT 1;
END

另一个想法是创建一个函数,并在函数中创建 if 块中的四个表填充表并返回表,但我真的不知道它是否可以工作或是否可能。但如果是下一个问题,如果有新列,我真的不想添加每个新列,所以我认为它不可能是自动的。

_Name is a VARCHAR(24)
_IP is VARCHAR(16)
_Date is INT(11)
4

0 回答 0