如何在mysql更新触发器中使用select查询

时间:2018-04-15 07:24:14

标签: mysql

描述:

我想在MySQL中使用select查询来更新触发器。

表格列:

1)订单:id,final_price,products_price,send_price,discount_price,turnover_id,turnover_credit_id

2)失误:id,价格

3)turnover_credits:id,price

我的结果:

以下查询正常运行:

CREATE TRIGGER test
BEFORE UPDATE ON orders
FOR EACH ROW
SET NEW.final_price = 
      NEW.product_price
    + NEW.send_price 
    - NEW.discount_price

但是以下查询失败了:

SET NEW.final_price = 
      NEW.total_price 
    + NEW.send_price 
    - NEW.discount_price 
    - SELECT IFNULL((SELECT t.price FROM turnovers AS t WHERE t.id=NEW.turnover_id), 0)
    - SELECT IFNULL((SELECT tc.price FROM turnover_credits AS tc WHERE tc.id=NEW.turnover_credit_id), 0)


SET NEW.final_price = 
      NEW.total_price 
    + NEW.send_price 
    - NEW.discount_price 
    - SELECT CAST(IFNULL((SELECT t.price FROM turnovers AS t WHERE t.id = NEW.turnover_id ) , 0 ) AS SIGNED ) 
    - SELECT CAST(IFNULL((SELECT tc.price FROM turnover_credits AS tc WHERE tc.id=NEW.turnover_credit_id), 0) AS SIGNED)

SET NEW.final_price = 
      NEW.total_price 
    + NEW.send_price 
    - NEW.discount_price 
    - CAST(SELECT CAST(IFNULL((SELECT t.price FROM turnovers AS t WHERE t.id=NEW.turnover_id), 0) AS SIGNED) AS SIGNED) 
    - CAST(SELECT CAST(IFNULL((SELECT tc.price FROM turnover_credits AS tc WHERE tc.id = NEW.turnover_credit_id ), 0) AS SIGNED) AS SIGNED)

运行上述查询后,MySQL错误信息为:

The following query has failed: "CREATE DEFINER... NEW.discount_price "
MySQL said: #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 
'SELECT CAST(IFNULL((SELECT t.price FROM turnovers AS t WHERE t.id=NEW.turnover_id), 0) AS SIGNED)' at line 8

请帮助我。

非常感谢。

0 个答案:

没有答案