如何在存储过程中连接多个select?

lg40wkob  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(306)

我需要发送登录\登录\尝试和登录\帐户\状态总是不管密码(passwd)匹配。只有密码匹配时,以下脚本才能正常工作。当它不匹配时,它发送所有null(但我尝试和状态)。当电子邮件地址正确但密码和/或状态不匹配时,发送登录尝试和登录帐户状态的最佳方式是什么?
我可以添加另一个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;
wgx48brx

wgx48brx1#

从我能猜到的问题中缺失的部分来看,你的代码运行得很好——这就是证据。

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)

如果你在做一些不同的事情,请充实你的问题并适当地评论。

相关问题