CREATE
    TRIGGER `beforeinsertevevtdata` 
    AFTER INSERT 
    ON `eventdata` 
    FOR EACH ROW BEGIN
SELECT COUNT(*) INTO @rownum FROM `eventdata_new` WHERE accountID = new.accountID;
IF(@rownum > 0) THEN
UPDATE `eventdata_new` SET timestamp = new.timestamp, latitude = new.latitude, longitude = new.longitude, altitude = new.altitude, TIME = new.time WHERE accountID = new.accountID;
ELSE
INSERT INTO `eventdata_new` (accoutID, TIMESTAMP, latitude, longitude, altitude, TIME) VALUES (new.accountID, new.timestamp, new.latitude, new.longitude, new.altitude, new.time);
END IF;

    END;我想在表eventdata插入数据时,在eventdata_new查询一下,当eventdata_new有accountIDeventdata插入的这条数据的accountID时UPDATE,没有时INSERT,但我这么写一只报错

解决方案 »

  1.   

    提示什么?
    如果accountID是主键的话,参考insert into ON DUPLICATE KEY UPDATE
      

  2.   

    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 '' at line 6 语法错误..........
      

  3.   

    DELIMITER $$
    CREATE
      TRIGGER `beforeinsertevevtdata`  
      AFTER INSERT  
      ON `eventdata`  
      FOR EACH ROW BEGIN
    SELECT COUNT(*) INTO @rownum FROM `eventdata_new` WHERE accountID = new.accountID;
    IF(@rownum > 0) THEN
    UPDATE `eventdata_new` SET `timestamp` = new.`timestamp`, latitude = new.latitude, longitude = new.longitude, altitude = new.altitude, `TIME` = new.time WHERE accountID = new.accountID;
    ELSE
    INSERT INTO `eventdata_new` (accoutID, `TIMESTAMP`, latitude, longitude, altitude, TIME) VALUES (new.accountID, new.timestamp, new.latitude, new.longitude, new.altitude, new.time);
    END IF;  END;
      $$
      

  4.   

    还是语法错误.........MySQL 返回: #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 '$$' at line 14 
      

  5.   

    头上的 DELIMITER $$ 会被自动截掉........
      

  6.   

    你是在MYSQL命令行下输入上述代码?
      

  7.   

    你是在什么中执行你的这个create trigger 语句的?
    建议直接在MYSQL行命令工具中试一下。另外建议直接用 insert into ... on duplicate 语句这样反而简单一些。 还有timestamp 等是关键字,尽量不要用来做字段名。CREATE TRIGGER `beforeinsertevevtdata`  
       AFTER INSERT  ON `eventdata`  
       FOR EACH ROW 
        INSERT INTO `eventdata_new` (accoutID, `TIMESTAMP`, latitude, longitude, altitude, `TIME`) 
        VALUES (new.accountID, new.timestamp, new.latitude, new.longitude, new.altitude, new.time)
        ON DUPLICATE KEY UPDATE `timestamp`=VALUES(timestamp),
        latitude=VALUES(latitude),
        longitude=VALUES(longitude),
        altitude=VALUES(altitude),
        `TIM`E=VALUES(TIME);
      

  8.   

    PHPmyadmin里输入的 ,后面少了DELIMITER ;不过加上DELIMITER ;后面报#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table' 汗,要什么版本才支持
      

  9.   


    不是版本的问题,是你的这个表上已经有了一个 AFTER INSERT 的触发器了。
      

  10.   

    你可以
    select * from INFORMATION_SCHEMA.TRIGGERS  where TRIGGER_NAME='beforeinsertevevtdata'检查一下有哪些触发器了。
      

  11.   

    不是报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 '' at line 6  
    就是报#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'  
    没有正确过,但的确有触发器被建立了............汗
      

  12.   

    不是报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 '' at line 6 这个是语法错。就是报#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table' 
    这个是已有触发器了。
      

  13.   

    如果是初学,建议浏览一下官方手册。
    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html