2017-07-06 80 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