Mysql分步向导失败

标签 mysql stored-procedures wizard

我正在使用 mysql 触发器和存储过程制作一个向导。到目前为止我已经有了这个

delimiter $$
CREATE TRIGGER le_trigger
AFTER INSERT ON inbox
FOR EACH ROW
BEGIN
declare last_inserted_number VARCHAR(100) DEFAULT '0800100200';
declare last_inserted_message VARCHAR(100) DEFAULT 'Lorem Ipsum';

set last_inserted_number = NEW.in_number;
set last_inserted_message = NEW.in_message;

if (not exists(select id from transactions where tel = last_inserted_number)) then
    insert into transactions(message, tel)
        values(last_inserted_message, last_inserted_number);
        insert into outbox(out_message, out_number)
        values("go to step 1", last_inserted_number);
else

   if ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_2='')) then
    update transactions set step_2=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 3", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_3='')) then
    update transactions set step_3=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 4", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_4='')) then
    update transactions set step_4=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 5", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_5='')) then
    update transactions set step_5=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 6", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_6='')) then
    update transactions set step_6=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 7", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_7='')) then
    update transactions set step_7=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 8", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_8='')) then
    update transactions set step_8=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 9", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_9='')) then
    update transactions set step_9=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("success.you have completed the form", last_inserted_number);
end if;

end if;

END$$
delimiter ;

但问题是所有的 if 都同时执行,并且没有实现类似向导的行为。计划是监视表收件箱并在插入触发器后抓取消息和发件人号码。是否可以退出当这是真的时循环

else

   if ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
end if;
exit..here

... more step_* code....

并在收件箱中再次插入后继续?.

最佳答案

不要将代码编写为单独的语句。使用 elseif (您可以阅读文档 here ):

if (not exists(select id from transactions where tel = last_inserted_number)) then
    insert into transactions(message, tel)
        values(last_inserted_message, last_inserted_number);
        insert into outbox(out_message, out_number)
        values("go to step 1", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_2='')) then
    update transactions set step_2=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 3", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_3='')) then
    update transactions set step_3=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 4", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_4='')) then
    update transactions set step_4=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 5", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_5='')) then
    update transactions set step_5=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 6", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_6='')) then
    update transactions set step_6=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 7", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_7='')) then
    update transactions set step_7=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 8", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_8='')) then
    update transactions set step_8=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 9", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_9='')) then
    update transactions set step_9=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("success.you have completed the form", last_inserted_number);
end if;

我不能 100% 确定这是您想要的逻辑。如果没有,它应该指导您该怎么做。

关于Mysql分步向导失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578753/

相关文章:

php - Laravel 选择默认禁用选项

sql - 优化 Oracle 存储过程

iphone - 用于在具有多个输入 View 的 iOS 中创建 "wizard"表单 View 的对象结构

c# - 如何使用 ASP.Net MVC 制作向导

MySQL 错误代码 1241

php - 将 SQL 函数作为 PDO 准备语句运行并返回值

php - 使用 PHP 从 MySQL 检索表的最佳方法是什么?

installation - 为我的 Windows 窗体应用程序安装向导 - 或不?

mysql - 选择城市最低价格的房间

javascript - 如何从 JavaScript 代码调用存储过程?