Mysql 如何引用转换表

Mysql 如何引用转换表,mysql,sql,triggers,Mysql,Sql,Triggers,我有两张桌子: create table possiede ( soc1 integer not null, soc2 integer not null, primary key (soc1,soc2), perc double ); create table contr ( soc1 integer not null, soc2 integer not null, primary key(soc1, soc2) ); 我有两个通用SQL语法的触发器,我需要将它们转换为MySQL语法: cr

我有两张桌子:

create table possiede (
soc1 integer not null,
soc2 integer not null,
primary key (soc1,soc2),
perc double
);

create table contr (
soc1 integer not null,
soc2 integer not null,
primary key(soc1, soc2)
);
我有两个通用SQL语法的触发器,我需要将它们转换为MySQL语法:

create trigger contrDir
after insert on possiede
for each row
when percent > 0.5 and (soc1,soc2) not in (select * from contr)
insert into contr values (soc1,soc2);

create trigger contrIndir
after insert on possiede
referencing new table as newTable
for each row
insert into possiede values
(select P.soc1, N.soc2, P.perc+N.perc
from newTable as N join possiede as P on N.soc1 = P.soc2);
这是我第一次尝试,但它给了我一个关于“引用”关键字的错误(“语法错误,引用意外的IDENT_,应为_SYM”),我不确定翻译是否正确:

create trigger controllo
after insert on possiede
REFERENCING new table as newTable
for each row
begin
    insert into possiede (select P.soc1, N.soc2, P.perc+N.perc from
    newTable as N join possiede as P on N.soc1=P.soc2);
    if percent > 0.5 and (soc1,soc2) not in (select * from contr) then
    insert into contr values (soc1,soc2);
    end if;
end;

正如您所注意到的,由于一些MySQL限制,我不得不将两个触发器压缩为一个。有人能给我正确的翻译吗?

请将列名放在大括号内,并使用
新表格
。此外,我认为IF块中的
In子句不正确,因为您正在对照
select*from…
检查两列(soc1和soc2)。请尝试使用更新的查询,如下所示:

  CREATE TRIGGER controllo
  AFTER INSERT on possiede
  REFERENCING NEW_TABLE AS newTable
  FOR EACH ROW
   BEGIN
      INSERT INTO possiede (soc1, soc2, perc) 
      SELECT P.soc1, N.soc2, P.perc+N.perc 
      FROM newTable AS N JOIN possiede AS P ON N.soc1=P.soc2;
      IF percent > 0.5 and soc1 not in (select soc1 from contr)
          and soc2 not in (select soc2 from contr)
        THEN
          INSERT INTO contr VALUES (soc1,soc2);
      END IF;
    END;

我不明白你的答案。我不需要输入mi表(soc1、soc2、perc),而是需要输入select查询返回的内容。我很确定这一点是正确的。@user1221297当你放大括号时,它需要列名称。请删除select查询周围的大括号。它不正确!您在第一次插入中消除了连接,我需要它来构建一种递归!而且,我在关键字“引用”上仍然有相同的错误。。似乎我不能在那个地方使用它。@user1221297:我不知道。现在检查答案,让我知道这是否有帮助。