如果存在MYSQL QUERY则获取该特定列值并与新值进行比较

时间:2017-07-06 19:26:28

标签: mysql sql triggers database-trigger

我试图将新值与基于SQL语句的数据中的现有值进行比较,即转到表获取特定值并检查值是否大于新值然后更新其他表,否则执行什么都没有。

我的问题是我无法将查询值转换为字符串以在if语句中进行比较。

我的代码:

IF (EXISTS (SELECT * FROM table_1 WHERE   Id =NEW.Id and  time = NEW.time ORDER BY price ASC LIMIT 1)) THEN  

        IF (price <  NEW.price) THEN
//----------^^^^^^----------------- there is one column in table_1 not able to convert it to string to check in if statement///

            UPDATE historical SET low_price = NEW.price  WHERE  id =NEW.Id and date_time = NEW.time;  

        END IF;
    END IF;

1 个答案:

答案 0 :(得分:2)

如果price是table_1中的一列,则可以将第二个if条件移动到第一个if条件。

IF (EXISTS (SELECT * FROM table_1 WHERE   Id =NEW.Id and  time = NEW.time and price <  NEW.price ORDER BY price ASC LIMIT 1)) THEN  

        UPDATE historical SET low_price = NEW.price  WHERE  id =NEW.Id and date_time = NEW.time;  

END IF;