"错误代码:1109。未知表' LeaderBoard'在字段列表"在MYSQL中

时间:2013-12-11 02:50:38

标签: mysql sql triggers

我开始收到错误:

错误代码:1109。字段列表中的未知表'LeaderBoard'

我实施了以下触发器后:

USE `broadside`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` TRIGGER `LeaderBoard_AINS` 
AFTER INSERT ON `LeaderBoard` 
FOR EACH ROW
BEGIN
    if ((broadside.LeaderBoard.username= NEW.username) AND (broadside.LeaderBoard.userscore< NEW.userscore))
    then
        DELETE FROM `broadside`.`LeaderBoard`
        WHERE (username = NEW.username AND userscore != NEW.userscore);
    end if;
    if (broadside.LeaderBoard.username= NEW.username AND broadside.LeaderBoard.userscore< NEW.userscore)
    then
        DELETE FROM `broadside`.`LeaderBoard`
        WHERE (username = NEW.username AND userscore != NEW.userscore);
    end if;
end

表名是LeaderBoard,数据库名称是broadside。它编译得很好,但是当我尝试插入一个元素并且触发器实际上被触发时,我得到上面显示的1109错误。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

在if条件中,删除引用表并直接比较值。

更改

IF (
    ( broadside.LeaderBoard.username = NEW.username )
    AND
    ( broadside.LeaderBoard.userscore < NEW.userscore )
) THEN  

IF (
    ( username = NEW.username ) AND ( userscore < NEW.userscore )
) THEN  
顺便说一句:为什么你需要第二个if条件?

参考 :MySQL论坛上的类似帖子 MySQL Forums :: Triggers :: #1109 - Unknown table 'xyz' in field list

答案 1 :(得分:0)

您必须执行以下操作:

...
IF EXISTS(SELECT NULL FROM `leaderboard` WHERE `username` = NEW.`username` AND `userscore` < NEW.`userscore`) THEN
...

但是,您会遇到如下错误:

Error Code : 1442
Can't update table 'leaderboard' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

尝试执行DELETE时,原因是:

  

B.5. MySQL 5.6 FAQ: TriggersB.5.9: Can triggers access tables?

     

触发器可以访问自己的表中的旧数据和新数据。一个触发器   也可以影响其他表,但不允许修改   已经被使用(用于读或写)的表   调用函数或触发器的语句。