我的Mysql函数总是返回true

时间:2017-05-04 11:36:43

标签: mysql

我是mysql的新手。我找不到符合我案例的答案。 我写了一个函数,当在“member_answer”中找到“apseudo”超过5次时,它应该返回 false 问题是这个函数总是返回True。你能帮帮我吗?

DELIMITER //

CREATE FUNCTION noMaxTryValideMail(theMail VARCHAR(150)) RETURNS BOOLEAN

    BEGIN 

            /*This function returns True when we have at most 5 times "a pseudo"    in member_answer        */
      DECLARE varEmail VARCHAR(150);
      DECLARE varPseudo VARCHAR(35);

      DECLARE nbTry SMALLINT; 
      DECLARE canContinueValidMail BOOLEAN DEFAULT TRUE;

      SET varEmail := theMail;
      SET varPseudo := NULL;

      SELECT pseudo INTO varPseudo FROM inscription_member WHERE email_1 = varEmail;
      SELECT COUNT(pseudo) INTO nbTry FROM member_answer WHERE pseudo =@varPseudo; 

      IF nbTry >0 AND nbTry <6 THEN 
        SET canContinueValidMail := TRUE;
        RETURN canContinueValidMail;


      ELSE

          SET canContinueValidMail:= FALSE;
          RETURN canContinueValidMail;

      END IF ;

    END 

2 个答案:

答案 0 :(得分:0)

对于这个存储过程似乎有点过分。除非我误解了您的问题,否则这样的内部联接可以替换您的存储过程。只需检查应用程序代码中的返回计数通常是一行。

SELECT COUNT(*) FROM inscription_member i INNER JOIN
member_answer m on m.pseudo = i.pseudo
 WHERE email_1 = ?

答案 1 :(得分:0)

在您的帮助下,我的代码成为了:

  CREATE FUNCTION maxTryValideMail2(theMail VARCHAR(150)) RETURNS BOOLEAN
        BEGIN 
            DECLARE nbtry SMALLINT;

            SELECT COUNT(*) INTO nbTry FROM inscription_member i INNER JOIN
            member_answer m on m.pseudo = i.pseudo
            WHERE email_1 = theMail ;

            return nbTry > 5;

        END 

谢谢。