在insert语句中使用case

时间:2014-04-02 15:43:41

标签: mysql

我正在尝试使用插入

中的case创建一些逻辑

这是我正在使用的代码,但

elseif ( exists(select id from transactions where tel = last_inserted_number && secure_using_payslip IS NULL)) then
    update transactions set secure_using_payslip=last_inserted_message where 
     tel=last_inserted_number;

     insert into messageout(messagetext, messageto)
        values("you choose not to secure with payslip.", last_inserted_number),(case when last_inserted_message = 'no');

     insert into messageout(messagetext, messageto)
        values("you choose to secure with payslip.", last_inserted_number),(case when last_inserted_message = 'yes');

       insert into messageout(messagetext, messageto)
        values("Are you formally employed or self employed?.", last_inserted_number);

代码失败了。我该如何使用案例?。

1 个答案:

答案 0 :(得分:1)

CASE表达式放入值中。

INSERT INTO messageout (messagetext, messageto)
VALUES (CASE last_inserted_message
            WHEN 'no' THEN "you choose not to secure with payslip."
            WHEN 'yes' THEN "you choose to secure with payslip."
            ELSE "Invalid choice."
        END, last_inserted_number);