如何在存储过程中加入多个SELECT?

时间:2018-12-14 04:46:31

标签: mysql asp.net

无论密码(PassWD)是否匹配,我都需要始终发送LOGIN_LOGIN_ATTEMPTS和LOGIN_ACCOUNT_STATUS。仅当密码匹配时,以下脚本才能正常工作。如果不匹配,它将发送所有null(但我尝试发送并发送状态)。当电子邮件地址正确但密码和/或状态不匹配时,发送LOGIN_LOGIN_ATTEMPTS和LOGIN_ACCOUNT_STATUS的最佳方法是什么?

我可以添加另一个SELECT语句,但最终结果应该是一行,其余字段为空。

以下来自我的存储过程。

SELECT
login.LOGIN_USER_ID,
login.LOGIN_EMAIL,
login.LOGIN_LOGIN_ATTEMPTS,
login.LOGIN_ACCOUNT_STATUS,
GROUP_CONCAT(user_role.USER_ROLE_ROLE SEPARATOR ',') AS ROLES,
user_role.USER_ROLE_STATUS 
FROM login 
INNER JOIN user_role ON 
user_role.USER_ROLE_USER_ID = login.LOGIN_USER_ID AND user_role.USER_ROLE_STATUS = @AccStatus 
WHERE login.LOGIN_EMAIL = @UserEmail AND login.LOGIN_ACCOUNT_STATUS = @AccStatus AND login.LOGIN_PASSWORD = @PassWD;

SET @USER_FOUND = found_rows();

UPDATE login SET 
LOGIN_ACCOUNT_STATUS = (SELECT CASE (LOGIN_LOGIN_ATTEMPTS>@LoginAttempts) WHEN 1 THEN 'LOCKED' ELSE 'ACTIVE' END),
LOGIN_LOGIN_ATTEMPTS = (SELECT CASE (@USER_FOUND) WHEN 0 THEN LOGIN_LOGIN_ATTEMPTS + 1 ELSE 0 END),
LOGIN_LAST_LOGIN_DATE = (SELECT CASE (@USER_FOUND) WHEN 1 THEN @TransactionDateTime ELSE LOGIN_LAST_LOGIN_DATE END),
LOGIN_LAST_LOGIN_LOCATION = null 
WHERE LOGIN_EMAIL=@UserEmail;

1 个答案:

答案 0 :(得分:1)

根据我能猜出的问题中缺少的内容,您的代码可以正常工作-这就是证明。

drop procedure if exists p;
delimiter $$

CREATE  PROCEDURE `p`(
    IN `inacount_status` int,
    IN `inuseremail` varchar(3),
    IN `inpasswd` varchar(3),
    IN `inLoginAttempts` int
)
begin

set @acount_status = inacount_status;
set @useremail = inuseremail;
set @passwd = inpasswd;
Set @LoginAttempts = inloginattempts;

SELECT
login.LOGIN_USER_ID,
login.LOGIN_EMAIL,
login.LOGIN_LOGIN_ATTEMPTS,
login.LOGIN_ACCOUNT_STATUS
FROM login 
WHERE login.LOGIN_EMAIL = @UserEmail AND login.LOGIN_ACCOUNT_STATUS = @AccStatus AND login.LOGIN_PASSWORD = @PassWD;

SET @USER_FOUND = found_rows();

select found_rows();

UPDATE login SET 
LOGIN_ACCOUNT_STATUS = (SELECT CASE (LOGIN_LOGIN_ATTEMPTS>@LoginAttempts) WHEN 1 THEN 'LOCKED' ELSE 'ACTIVE' END),
LOGIN_LOGIN_ATTEMPTS = (SELECT CASE (@USER_FOUND) WHEN 0 THEN LOGIN_LOGIN_ATTEMPTS + 1 ELSE 0 END),
#LOGIN_LAST_LOGIN_DATE = (SELECT CASE (@USER_FOUND) WHEN 1 THEN @TransactionDateTime ELSE LOGIN_LAST_LOGIN_DATE END),
LOGIN_LAST_LOGIN_LOCATION = null 
WHERE LOGIN_EMAIL=@UserEmail;

end $$

delimiter ;

drop table if exists login;
create table login
(LOGIN_USER_ID int,
lOGIN_EMAIL varchar(3),
LOGIN_LOGIN_ATTEMPTS int,
LOGIN_ACCOUNT_STATUS varchar(10),
login_password varchar(3),
login_last_login_location varchar(10));

insert into login values
(1,'aaa',0,0,'xxx','loc');
insert into login values
(2,'bbb',0,0,'yyy','loc');

set @acount_status = 0;
set @useremail = 'aaa';
set @passwd = 'bbb';
Set @LoginAttempts = 2;

call p(@account_status,@useremail,@passwd,@loginattempts);
select * from login;

  +---------------+-------------+----------------------+----------------------+----------------+---------------------------+
| LOGIN_USER_ID | lOGIN_EMAIL | LOGIN_LOGIN_ATTEMPTS | LOGIN_ACCOUNT_STATUS | login_password | login_last_login_location |
+---------------+-------------+----------------------+----------------------+----------------+---------------------------+
|             1 | aaa         |                    1 | ACTIVE               | xxx            | NULL                      |
|             2 | bbb         |                    0 | 0                    | yyy            | loc                       |
+---------------+-------------+----------------------+----------------------+----------------+---------------------------+
2 rows in set (0.02 sec)

如果您以任何方式做不同的事情,请充实您的问题并适当评论。