是否有可能创建一个涉及3个表和条件语句的触发器?

时间:2012-02-28 07:09:04

标签: php mysql triggers

我的数据库中有3个表,它们都相互依赖

有一个项目表

+-----------+---------------------+-------------------------------------------------+
| id        | project name        |status (1 confirmed / 2 in progress / 3 done )   |
+-----------+---------------------+-------------------------------------------------+
|   233     | project 1           |1                                                |
+-----------+---------------------+-------------------------------------------------+
|  234      | project 2           | 3                                               |
+-----------+---------------------+-------------------------------------------------+

表2是类别

+-----------+---------------------+-------------------+-------------------+-------------------+ 
| id        | category            | confirmed_counter | inprogress_counter|done_counter       |
+-----------+---------------------+-------------------+-------------------+-------------------+
|         1 | c++                 |      2            |3                  | 1                 |
+-----------+---------------------+-------------------+-------------------+-------------------+
|         2 | php                 |      3            |4                  | 6                 |
+-----------+---------------------+-------------------+-------------------+-------------------+

和表3是类别对象

+-----------+---------------------+------------+ 
| id        | project_id          | category_id|
+-----------+---------------------+------------+
|         1 | 233                 |      2     |
+-----------+---------------------+------------+
|         2 | 244                 |      3     |
+-----------+---------------------+------------+

我希望在项目状态发生变化时创建一个更新类别计数器的触发器

当管理员确认某个项目时,它的状态将更改为1,因此在类别表中 confirmed_counter应该增加 但首先我必须查看类别对象并查看该项目的类别或类别,然后更新这些类别的计数器字段

$c = $project->get_categorys();
///// it returns array of that project category ids ( 2 ,3 ,4)
$sql = update `categorys` set `confirmed_counter` = confirmed_counter+1 where `id` in (2,3,4);

当项目正在进行时

$sql = update `categorys` set `confirmed_counter` = confirmed_counter-1 , `inprogress_counter` = inprogress_counter+1 where `id` in (2,3,4);

当项目完成时

$sql = update `categorys` set `inprogress_counter` = inprogress_counter-1 , `done_counter` = done_counter+1 where `id` in (2,3,4);

它并不总是按顺序排列 项目可能会在正在进行时重置

所以在更新计数器之前有两个重要的步骤

首先我必须从第三个表中获取项目的类别ID

然后我必须根据项目的当前和以前的状态增加和减少计数器

是否可以创建触发器来执行此操作?

/////////////////////////////////////////////// ///编辑///////////////////////////////////

是否可以代替仅仅假设前一个状态,在代码中使用OLD来指定? 喜欢改变

  SET
    confirmed_counter = 
      CASE NEW.status
        WHEN 1 THEN confirmed_counter + 1
        WHEN 2 THEN confirmed_counter - 1
        ELSE confirmed_counter
      END,

  SET
    confirmed_counter = 
      CASE NEW.status
        WHEN 1 THEN confirmed_counter + 1
        ELSE confirmed_counter
      END,

    confirmed_counter = 
      CASE OLD.status
        WHEN 1 THEN confirmed_counter - 1
        ELSE confirmed_counter
      END,

我试图像这样使用它但总是最后一个语句似乎被执行

1 个答案:

答案 0 :(得分:1)

是的可能 -

CREATE TRIGGER trigger1
  AFTER UPDATE
  ON project
  FOR EACH ROW
BEGIN
  UPDATE category c
    JOIN category_objects co
      ON co.category_id = c.id
  SET
    confirmed_counter = 
      CASE NEW.status
        WHEN 1 THEN confirmed_counter + 1
        WHEN 2 THEN confirmed_counter - 1
        ELSE confirmed_counter
      END,
    inprogress_counter =
      CASE NEW.status
        WHEN 1 THEN inprogress_counter
        WHEN 2 THEN inprogress_counter + 1
        ELSE inprogress_counter - 1
      END,
    done_counter =
      CASE NEW.status
        WHEN 1 THEN done_counter
        WHEN 2 THEN done_counter
        ELSE done_counter + 1
      END
  WHERE
    co.project_id = NEW.id;
END

关于第二个问题的例子:

...
confirmed_counter =
  CASE
    WHEN NEW.status = 1 THEN confirmed_counter + 1
    WHEN OLD.status = 1 THEN confirmed_counter - 1
    ELSE confirmed_counter
END
....