then-else的sql-trigger(mysql)不起作用

pb3skfrl  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(225)

我有一个触发器,它不会插入或更新“privilegio”,也不会在日志中给出错误

delimiter //

CREATE TRIGGER Privilegio_Pagamento AFTER insert
ON pagamento FOR EACH ROW
BEGIN
    declare numero int(8);
    declare ordutente varchar(64);

    select count(*) into numero from pagamento where ordinante = new.ordinante and opera = new.opera;
    select username into ordutente from utente where email = new.ordinante;
    if (numero <> 0) then
        update privilegio P set P.volumi = concat(P.volumi, '-', new.volumi) where opera = new.opera and utente = ordutente;
    else 
        insert into privilegio(utente, opera, volumi) values(ordutente, new.opera, new.volumi);
    end if;
END

有趣的是,每一行都是单独工作的(selects、updates、insert),所以失败的似乎是if-then-else,因为如果我使用这样的东西

delimiter //

CREATE TRIGGER Privilegio_Pagamento AFTER insert
ON pagamento FOR EACH ROW
BEGIN
    declare numero int(8);
    declare ordutente varchar(64);

    select count(*) into numero from pagamento where ordinante = new.ordinante and opera = new.opera;
    select username into ordutente from utente where email = new.ordinante;
    insert into privilegio(utente, opera, volumi) values(ordutente, new.opera, new.volumi);
END

它添加了“privilegio”。
当然我也试过这个

select if (((select count(*) from pagamento where ordinante = 'abel.ligure73@virgilio.it' and opera = 'Problema Grande e Dama Rossa') <> 0), 'yes', 'no')

这很管用。有什么问题吗?
编辑
戈登·林诺夫的回答是正确的,但就我的目的而言,它需要一个这样的concat concat(volumi, '-', new.volumi) . 希望别人会觉得有用!

bvn4nwqk

bvn4nwqk1#

我不完全理解逻辑,因为你是在数列 pagamento 然后 insert ing公司/ update 正在插入 privilegio . 这似乎不对。
事实上,整个逻辑似乎可以用 on duplicate key update :

insert into privilegio (utente, opera, volumi)
    select u.username, new.opera, new.volumi
    from utente u
    where u.email = new.ordinante
    on duplicate key update volumi = concat(volumi, '-', new.volumi);

相关问题