MySQL触发器语法错误联合两个/三个表

时间:2012-11-30 10:26:48

标签: mysql sql triggers

我写了以下代码:

create trigger money after update on `things`
for each row
begin
   Select @c1=sum(`thing_cost`) from `things`
   UNION
   Select @c2=sum(`salary`) from `dude_base`
   Update `current` set `curr_cash`=@c1*@c2/100
end;
$$

表“事情”得到了:

id1 (PK)
name
thing_cost

表dude_base已经:

id2 (PK)
salary
name, etc. irrevelant

表电流已经得到:

id1 (FK)
id2(FK)
curr_cash

我收到了以下错误:

 #1064 - You have an error in your SQL syntax; check the manual that corresponds to   
 your MySQL server version for the right syntax to use
 near 'Update `current` set `curr_cash`=@c1*@c2/100; END' at line 7

任何帮助?

1 个答案:

答案 0 :(得分:1)

我认为你应该有一个分号;喜欢

Select @c2=sum(`salary`) from `dude_base`;

结束此查询,因为UPDATE是另一个查询

//编辑

尝试这样

SET @c1 = (SELECT sum(`thing_cost`) from `things`);
SET @c2 = (SELECT sum(`salary`) from `dude_base`);
UPDATE `current` SET `curr_cash` = @c1 * @c2 / 100