将SQL更新语句转换为View语句

时间:2014-08-03 05:20:14

标签: mysql sql view

我有一个表的更新语句公式,并且该语句有效,但我想每次添加新数据时自动更新我的表,所以我认为我应该使用View。

这是我的更新声明:

followers f left outer join
       followers f2
       on f2.id = f.id
    set f.Growth = f.Growth_Speed/(SELECT AVG(f.Growth_Speed) FROM (SELECT f.Growth_Speed from followers f WHERE f.id <= f.id+1));

这是我的View声明:

CREATE VIEW `followers_view` AS
    select 
        `followers`.`id` AS `id`,
        `followers`.`date` AS `date`,
        `followers`.Growth_Speed` AS `Growth_Speed`
    From `my_database.followers`;

我想将该更新语句添加为View中的第四列,但不确定如何执行此操作。

由于

1 个答案:

答案 0 :(得分:0)

您必须在插入新数据时将Trigger设置为要进行更新的表

代码将是这样的:

CREATE TRIGGER update_table AFTER INSERT ON followers
        FOR EACH ROW
            BEGIN
              followers f left outer join followers f2   on f2.id = f.id
              set f.Growth = f.Growth_Speed/(SELECT AVG(f.Growth_Speed) FROM (SELECTf.Growth_Speed from followers f WHERE f.id <= f.id+1));            

            END

我猜是关注者是您插入数据的表,以及您要更新的数据。