开发者

MySQL IF...THEN followed by Query Statement

Is something like this valid in MySQL:

DELIMITER //
CREATE FUNCTION LOGIN_VALIDATE(IN uName VARCHAR(32),IN uPass VARCHAR(32))
RETURNS BOOLEAN
BEGIN
    DECLARE row_count BOOLEAN DEFAULT 0;
    DECLARE stored_pass VARCHAR(32) DEFAULT NULL;
    DECLARE valid_return BOOLEAN DEFAULT 0;
    SELECT count(*) INTO row_count FROM Login WHERE userId='uName';
    IF row_count = 1 THEN 
        SELECT pWord INTO stored_pass FROM Login WHERE userId='uName';
    END IF;
    IF stored_pass = 'uPass' THEN 
        SET valid_return = 1;
    END IF;
    RETURN开发者_如何学运维 valid_return;
END //
DELIMITER ;


Use:

DECLARE valid_return BOOLEAN DEFAULT 0;

SELECT CASE WHEN COUNT(*) = 1 THEN 1 ELSE 0 END
  INTO valid_return
  FROM LOGIN l
 WHERE l.userid = @uName
   AND l.pword = @uPass

Always try to minimize the number of passes over a table to get your desired result. SQL is SET based, not procedural - SQL will work better for you when it is approached correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜