2017-07-06 79 views
0

我试图用一个SQL语句中的数据来比较一个新值与现有值,即转到表中获取特定值并检查值大于新值然后更新其他表,否则什么都不做。MYSQL QUERY if exists then get that specific column value and compare with new value

我的问题是,我无法将查询值转换为字符串以在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; 

回答

2

如果价格是TABLE_1一列,可以将第二如果条件移至第一,如果条件。

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; 
+0

感谢我尝试了答复,但它是像我想在一个字符串值,因为我用两个操作中是>和一个<当时它感到困惑,并创建其执行问题 – Shaik

+0

在错误的时间,当它不应该 – Shaik

+0

“WHERE(price as varchar) NNguyen