如何在 mysql 存储过程中使用 case-when 语句?

发布于 2024-10-19 22:39:10 字数 564 浏览 7 评论 0原文

我想使用 request_time 参数自动设置 session_id,因此我选择了包含 case 语句的 mysql 存储过程。就这样。

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 

begin 

update user_input;

case request_time

when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' 
then set user_session_id = 1; 

when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' 
then set user_session_id =2; 

end case; 
end
//

但是我在 // 之后输入时收到 1064 错误。我检查了 mysql 文档,我认为 case 语法是正确的。

请帮忙。

I want to set the session_id automatically using the request_time parameter so i opted for a mysql stored procedure that contains a case statement.Here goes.

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 

begin 

update user_input;

case request_time

when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' 
then set user_session_id = 1; 

when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' 
then set user_session_id =2; 

end case; 
end
//

However i get a 1064 error on enter after //. I have checked the mysql documentation i think the case syntax is correct.

Help please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-10-26 22:39:10
delimiter //

create procedure upd_userinput(in request_time TIMESTAMP,out time TIME, out user_session_id INT)     

begin      

set time = time(request_time);    

case  
when time < '09:15:00' && time > '08:15:00' then      
set user_session_id = 1; 

when time < '10:15:00' && time > '11:15:00' then
set user_session_id = 2;  

else set user_session_id =3;

end case;  
end //
delimiter //

create procedure upd_userinput(in request_time TIMESTAMP,out time TIME, out user_session_id INT)     

begin      

set time = time(request_time);    

case  
when time < '09:15:00' && time > '08:15:00' then      
set user_session_id = 1; 

when time < '10:15:00' && time > '11:15:00' then
set user_session_id = 2;  

else set user_session_id =3;

end case;  
end //
清晨说晚安 2024-10-26 22:39:10

大小写必须至少匹配给定的语句之一,否则您将收到此错误。为了避免错误,请在其中包含 else。

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 
begin 
update user_input;
case request_time
when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' then
set user_session_id = 1; 
when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' then
set user_session_id =2; 
else set user_session_id =0; 
end case; 
end

the case must match at least one of the given statements, otherwise you will get this error. To avoid the error include an else in it.

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 
begin 
update user_input;
case request_time
when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' then
set user_session_id = 1; 
when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' then
set user_session_id =2; 
else set user_session_id =0; 
end case; 
end
找回味觉 2024-10-26 22:39:10

在 mysql 存储过程中使用 case-when 语句的最佳方法是:

DELIMITER $
CREATE PROCEDURE my_pro()
BEGIN
UPDATE users 
SET phone = 
(
    CASE 
        WHEN substr(phone, 2,1) = '3' THEN concat('04', substr(phone, 2))
        WHEN substr(phone, 2,1) = '2' THEN concat('05', substr(phone, 2))
    END
);
END $

The best way To use a case-when statement in a mysql stored procedure is:

DELIMITER $
CREATE PROCEDURE my_pro()
BEGIN
UPDATE users 
SET phone = 
(
    CASE 
        WHEN substr(phone, 2,1) = '3' THEN concat('04', substr(phone, 2))
        WHEN substr(phone, 2,1) = '2' THEN concat('05', substr(phone, 2))
    END
);
END $
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文