如果有条件的mysql触发器?

时间:2017-07-30 17:56:29

标签: mysql if-statement triggers

直接前进,我在触发器mysql中使用条件状态时遇到问题。 如果状态为“来宾”,则有2种状态。然后做点什么......还做另一件事。

这是我已经做过的事情:

CREATE TRIGGER `update-gain` AFTER INSERT ON `payment`
FOR EACH ROW BEGIN
declare v_profit integer;
declare v_deposit integer;
declare v_status CHAR;

select (status) into v_status from item where item.id_item=new.id_item;
select sum(profit_owner) into v_profit from payment where payment.id_item=new.id_item;
select sum(profit_guest ) into v_deposit from payment where payment.id_item=new.id_item;

if v_status like 'guest' then
INSERT INTO gain
(gain.profit, gain.modal,gain.date)
VALUES
(v_profit,v_deposit,new.date)
ON DUPLICATE KEY UPDATE
gain.profit = gain.profit+new.profit_owner,
gain.modal = gain.deposit+new.profit_guest;

else
INSERT INTO gain
(gain.profit,gain.tanggal)
VALUES
(v_profit,new.tgl_stok)
ON DUPLICATE KEY UPDATE
gain.profit = gain.profit_owner+new.profit_owner;

end if;
END

到目前为止,我正在使用'喜欢'条款,但条件总是如此,即使v_status不是' guest'。

我在这里做错了什么? 谢谢:))

1 个答案:

答案 0 :(得分:0)

变化

if v_status like 'guest' then

代表

if v_status = 'guest' then