MySQL Trigger包括表连接

时间:2016-02-04 01:27:39

标签: mysql sql

我需要在MySQL中为phpmyadmin编写一个触发器,它将数据从一个名为RefTable的表中插入两个单独的表(IssueTable和JobTable)。从RefTable中返回一个正布尔值后,我的触发器在我的Issue表中工作,并且正在插入数据。 JobTable插入证明是困难的。

在从RefTable插入JobTable之前,我需要检查另一个名为ProjectTable的表中的布尔值;如果这返回肯定,那么我想将数据插入JobTable。

一旦我有两个查询也需要加入这两个查询,因为phpmyadmin不能用于多个触发器。

我在数据库中插入数据的查询如下:

CREATE TRIGGER To_Issue_Table
AFTER INSERT ON _RefTable

BEGIN

Declare EntryDate DATETIME;
SET EntryDate = CURDATE();

CASE WHEN (new.ServiceCall) = 1
THEN INSERT INTO `_IssueTable`( `RefID`, `IssueEntryDate`, `IssueDesc`, `ResponseID`)
VALUES
( new.RefID, EntryDate, new.ProjectScope, new.ResponseID);
END CASE;

END

如下解决 - 一个数据领域没有复制,但还没有什么大不了的。 我已经修改了查询,它正在提交并且正在填充IssueTable,但是JobTable不是:

BEGIN

Declare EntryDate DATETIME;
Declare SurveyReqVar INT;
Declare CurrentProjectID INT;
SET EntryDate = CURDATE();
SET CurrentProjectID = new.ProjectID;
SET SurveyReqVar = 
(SELECT `SurveyReq`
        FROM _ProjectTable
        WHERE ProjectID = CurrentProjectID);

CASE WHEN (new.ServiceCall) = 1
THEN INSERT INTO `_IssueTable`( `RefID`, `IssueEntryDate`, `IssueDesc`, `ResponseID`)
VALUES
( new.RefID, EntryDate, new.ProjectScope, new.ResponseID);

WHEN (SurveyReqVar) = 1
THEN INSERT INTO `_JobTable`( `RefID`, `JobDetails`, `JobEntryDate`)
VALUES
( new.RefID, new.ProjectScope, EntryDate);



END CASE;

END

1 个答案:

答案 0 :(得分:0)

如下解决,一个数据字段没有复制,但其余的都没有。

BEGIN

Declare EntryDate DATETIME;
Declare SurveyReqVar INT;
Declare CurrentProjectID INT;
SET EntryDate = CURDATE();
SET CurrentProjectID = new.ProjectID;
SET SurveyReqVar = 
(SELECT `SurveyReq`
        FROM _ProjectTable
        WHERE ProjectID = CurrentProjectID);

CASE WHEN (new.ServiceCall) = 1
THEN INSERT INTO `_IssueTable`( `RefID`, `IssueEntryDate`, `IssueDesc`, `ResponseID`)
VALUES
( new.RefID, EntryDate, new.ProjectScope, new.ResponseID);

WHEN (SurveyReqVar) = 1
THEN INSERT INTO `_JobTable`( `RefID`, `JobDetails`, `JobEntryDate`)
VALUES
( new.RefID, new.ProjectScope, EntryDate);



END CASE;

END