2013-07-01 38 views
0

我有两个名为security_questions并登录的表。在登录表中的列有:加入两个表,然后搜索匹配

Username, Security_QA_ID, Security_Answer, Security_Attempts,Last_Login,Password_Attempts 

其中security_questions有:

ID, Name 

其中Security_QA_ID与ID

CREATE DEFINER=`satish`@`%` PROCEDURE `p_chkAnswer`(
IN sq VARCHAR(75), 
IN sa VARCHAR(20) , 
IN uname VARCHAR(15) , 
out msg INT 
) 
BEGIN 
select (COUNT(*) > 0) INTO @result from login join security_questions on Security_QA_ID = ID where User_Name=uname and Name=sq and Security_Answer=sa; 
set msg = @result; 
if @result = 1 Then 
UPDATE login SET Last_Login=now(),Password_Attempts=0 where User_Name=uname; 
else 
UPDATE login SET Security_Attempts=Security_Attempts+1 where User_Name=uname; 
End if; 

END 

但每次只else部分得到执行引用。在此先感谢

回答

0

您的代码是相当混乱,难以阅读(至少对我来说),我简化了您的声明到一个单一的SELECT,以便我可以检查您的选择是否正确,它看起来是正确的:

CREATE TABLE `login` 
(User_Name text, 
    Security_QA_ID int(11), 
    Security_Answer text, 
    Security_Attempts datetime, 
    Last_Login datetime, 
    Password_Attempts int(11) 
); 

CREATE TABLE `security_questions` 
(ID int(11), 
    Name text 
); 

INSERT INTO login SET 
User_name = 'a', 
Security_QA_ID = 1, 
Security_Answer = 'c', 
Security_Attempts = NOW(), 
Last_Login = NOW(), 
Password_Attempts = 0; 

INSERT INTO security_questions SET 
ID = 1, 
Name = 'b'; 

和选择

SELECT IF(COUNT(*) > 0, 1, 0) AS Authenticated 
from (login) 
LEFT join security_questions on Security_QA_ID = ID 
where User_Name='a' and Name='b' and Security_Answer='c' 

你就必须使用这个DEFINER?编写PHP/Perl/etc代码来检查认证的是不是非零?

+0

感谢您的帮助,实际上我是一个初学者,在mySql中。我没有得到你想要做的事情。什么是身份验证。它的一个关键字或什么 – Subodh

+0

我要求MySQL ** count()**从我的** SELECT **返回多少条记录。并将其作为一个名为** Authenticated **的变量返回(我可以称它为CountMePlease,它只是一个名称)。我的SQL只是找到你得到的行数,0表示用户没有提供正确的答案,1或更多意味着他给出了正确的答案。 – nrathaus

+0

同样的结果。只有其他部分正在执行 – Subodh

相关问题