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

时间:2015-08-04 12:03:24

标签: php mysql

是我的触发器,但它不起作用。

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;

我需要根据提交的值更新列。

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

等。 有没有办法如何根据NEW.region值更新列?

1 个答案:

答案 0 :(得分:1)

您无法创建新的列名称。对于update和其他SQL语句一样。您可以明确列出更新列:

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;