简体   繁体   English

MySQL触发器:根据提交的值更新列

[英]MySQL trigger: updating column depending of a submitted value

Ths my trigger but it doesn't work. 这是我的触发器,但是不起作用。

CREATE TRIGGER events_events_a BEFORE INSERT ON events_events
FOR EACH ROW
BEGIN 
IF NEW.public = 1 THEN
UPDATE events_cats SET etotal=etotal+1, etotal_NEW.region=etotal_NEW.region+1 WHERE id=NEW.category;
END IF;
END;

I need to update column depending of a submitted value. 我需要根据提交的值更新列。

if NEW.region value is 1, I need to update column etotal_1=etotal_1+1
if NEW.region value is 2, I need to update column etotal_2=etotal_2+1
if NEW.region value is 3, I need to update column etotal_3=etotal_3+1

etc. Is there any way how to update column depending of NEW.region value? 等等。有什么方法可以根据NEW.region值更新列吗?

You cannot create a new column name. 您无法创建新的列名。 This is as true for update as for other SQL statements. update和其他SQL语句一样。 You can explicitly list the columns for the update: 您可以显式列出更新的列:

UPDATE events_cats
    SET etotal = etotal + 1,
        etotal_1 = (case when new.region = 1 then etotal_1 + 1 else etotal_1 end),
        etotal_2 = (case when new.region = 2 then etotal_2 + 1 else etotal_1 end),
        etotal_3 = (case when new.region = 3 then etotal_3 + 1 else etotal_1 end)
    WHERE id = NEW.category;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM