如何在mysql触发器中设置值

时间:2018-07-04 13:10:23

标签: mysql

这是mysql查询,我想将avg设置为变量,并且mb.HELP有时为null,我想关闭此刻度。如果其空值除以3,则不除以4。也用该值将值设置为其他列,但查询语法错误类似;

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本以使用正确的语法   接近'IF mb.Dynamic_Help IS NULL THEN           SET @newavg:=(第6行的mb.Dynamic_Delivery_Sp'

查询;

CREATE TRIGGER computeColor
AFTER INSERT ON merchant_rates
FOR EACH ROW

   UPDATE merchant_branches as mb
    IF mb.Dynamic_Help  IS NULL THEN
        SET @newavg := (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3
     ELSE 
        SET @newavg := (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4
    END IF;


CASE
    WHEN @newavg >= 9   THEN  mb.Dynamic_Rank_Color = "#FFD300"
   WHEN @newavg >= 8,5  AND @newavg < 9     THEN  mb.Dynamic_Rank_Color = "#FFD300"
   WHEN @newavg >= 8    AND @newavg < 8,5   THEN  mb.Dynamic_Rank_Color = "#FFD320"
   WHEN @newavg >= 7,5  AND @newavg < 8     THEN  mb.Dynamic_Rank_Color = "#FAD300"
   WHEN @newavg >= 7    AND @newavg < 7,5   THEN  mb.Dynamic_Rank_Color = "#CCD300"
   WHEN @newavg >= 6,5  AND @newavg < 7     THEN  mb.Dynamic_Rank_Color = "#FED300"
END
    WHERE mb.id = NEW.Branch_ID

1 个答案:

答案 0 :(得分:0)

您可以将update子句操纵为-

UPDATE merchant_branches as mb
    SET mb.Dynamic_Rank_Color = CASE 
                                WHEN mb.Dynamic_Help IS NOT NULL
                                THEN
                                    CASE 
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 >= 9 THEN "#FFD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 BETWEEN 9 AND 8.5 THEN "#FFD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 BETWEEN 8.5 AND 8 THEN "#FFD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 BETWEEN 8 AND 7.5 THEN "#FAD320"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 BETWEEN 7.5 AND 7 THEN "#CCD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service+mb.Dynamic_Help)/4 BETWEEN 7 AND 6.5 THEN "#FED300"
                                    END
                                WHEN mb.Dynamic_Help IS NULL
                                THEN
                                    CASE 
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 >= 9 THEN "#FFD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 BETWEEN 9 AND 8.5 THEN "#FFD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 BETWEEN 8.5 AND 8 THEN "#FFD320"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 BETWEEN 8 AND 7.5 THEN "#FAD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 BETWEEN 7.5 AND 7 THEN "#CCD300"
                                    WHEN (mb.Dynamic_Delivery_Speed + mb.Dynamic_Flavor + mb.Dynamic_Service)/3 BETWEEN 7 AND 6.5 THEN "#FED300"
                                    END
                                END
    WHERE mb.id = NEW.Branch_ID