存储过程无法正常运行

时间:2014-07-17 11:57:12

标签: mysql stored-procedures

我试图制作一个存储过程来计算黄金,白银等排名系统上的业务点。我做了这个程序,但是它给出了我的错误而且无法弄清楚问题是什么。它说检查关于查询的mysql语法,但是当我单独运行查询时它们运行正常。任何人都可以帮我弄清楚问题是我做错了什么。他是程序新手。这应该是从前端完成但现在它应该从后端数据库完成。 请帮助和建议

 DELIMITER //
 CREATE procedure weightedavg(IN  businessid INT(11))
 BEGIN
 SET @basic = 1;
 SET @silver = 2;
 SET @gold = 4;
 SET @platinum = 8;
 SET @diamond = 16;

 select @badgegold:=@badgegold user_badge_id from user_badge_masters where        
 user_badge_desc="Gold";
 select @badgePlatinum:=@badgePlatinum user_badge_id from user_badge_masters where      
 user_badge_desc="Platinum";
 select @badgeSilver:=@badgeSilver user_badge_id from user_badge_masters where    
 user_badge_desc="Silver";
 select @badgeDiamond:=@badgeDiamond user_badge_id from user_badge_masters where     
 user_badge_desc="Diamond";
 select @badgeBasic:=@badgeBasic user_badge_id from user_badge_masters where    
 user_badge_desc="Basic";



  SET @sum_valg= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and      
  `review_trans`.`user_badge_id`=@badgegold
        GROUP BY `review_trans`.`user_badge_id`;




  SET @sum_valp= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and    
  `review_trans`.`user_badge_id`=@badgePlatinum
        GROUP BY `review_trans`.`user_badge_id`;

        SET @sum_vals= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and 
  `review_trans`.`user_badge_id`=@badgeSilver
        GROUP BY `review_trans`.`user_badge_id`;

       SET @sum_vald= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and 
   `review_trans`.`user_badge_id`=@badgeDiamond
        GROUP BY `review_trans`.`user_badge_id`;

       SET @sum_valb= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and 
    `review_trans`.`user_badge_id`=@badgeBasic
        GROUP BY `review_trans`.`user_badge_id`;


        SET @sum_valg_count=select count(user_id) from review_trans where   
   user_badge_id=1;  
         SET @sum_vals_count=select count(user_id) from review_trans where 
   user_badge_id=4;  
          SET @sum_valp_count=select count(user_id) from review_trans where 
   user_badge_id=3;  
           SET @sum_valb_count=select count(user_id) from review_trans where 
   user_badge_id=6;  
            SET @sum_vald_count=select count(user_id) from review_trans where 
   user_badge_id=5;





        IF @sum_valg=0 THEN
        set @gold_avg=0
        else
        set @gold_avg=@sum_valg/@sum_valg_count;


          IF @sum_valp=0 THEN
        set @platinum_avg=0
        else
        set @platinum_avg=@sum_valp/@sum_valp_count;



          IF @sum_vals=0 THEN
        set @silver_avg=0
        else
        set @silver_avg=@sum_vals/@sum_vals_count;

        IF @sum_vald=0 THEN
        set @diamond_avg=0
        else
        set @diamond_avg=@sum_vald/@sum_vald_count;

        IF @sum_valb=0 THEN
        set @basic_avg=0
        else
        set @basic_avg=@sum_valb/@sum_valb_count;


        IF @gold_avg <=0 THEN
        set @gold_effectiveweight=0
        else
        set @gold_effectiveweight=@basic;

        IF @platinum_avg <=0 THEN
        set @platinum_effectiveweight=0
        else
        set @platinum_effectiveweight=@platinum;


         IF @silver_avg <=0 THEN
        set @silver_effectiveweight=0
        else
        set @silver_effectiveweight=@silver;


         IF @diamond_avg <=0 THEN
        set @diamond_effectiveweight=0
        else
        set @diamond_effectiveweight=@diamond;


        IF @basic_avg <=0 THEN
        set @basic_effectiveweight=0
        else
        set @basic_effectiveweight=@basic;


        set @total_weight=@gold_effectiveweight+@platinum_effectiveweight+
        @silver_effectiveweight+@diamond_effectiveweight+@basic_effectiveweight;

     set @basic_weighted_score=@basic_effectiveweight/@total_weight;
     set @silver_weighted_score=@silver_effectiveweight/@total_weight;
     set @gold_weighted_score=@gold_effectiveweight/@total_weight;
     set @plat_weighted_score=@platinum_effectiveweight/@total_weight;
     set @diam_weighted_score=@diamond_effectiveweight/@total_weight;


      set @avg_basic_weighted_score=@basic_weighted_score*@basic_avg;
      set @avg_silver_weighted_score=@silver_weighted_score*@silver_avg;
      set @avg_gold_weighted_score=@gold_weighted_score*@gold_avg;
      set @avg_plat_weighted_score=@plat_weighted_score*@platinum_avg;
      set @avg_diam_weighted_score=@diam_weighted_score*@diamond_avg;

          @business_point=@avg_basic_weighted_score+ @avg_silver_weighted_score+
      @avg_gold_weighted_score+@avg_plat_weighted_score+@avg_diam_weighted_score;

          @business_point=TRUNCATE(@business_point,1);
          UPDATE businessmasters SET business_point=@business_point WHERE    
          business_id=businessid;


  END;//
     Delimiter ;

1 个答案:

答案 0 :(得分:0)

您必须在括号中编写SELECT语句才能工作。 而不是

SET @sum_valg= select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and      
  `review_trans`.`user_badge_id`=@badgegold
        GROUP BY `review_trans`.`user_badge_id`;

你应该使用

SET @sum_valg=(select SUM(review_trans.rating_given) as sum
        from `review_trans` where  `review_trans`.`business_id`=businessid  and      
  `review_trans`.`user_badge_id`=@badgegold
        GROUP BY `review_trans`.`user_badge_id`);

然后它应该工作。 您必须对要在其中分配查询计算值的所有SET语句执行此操作。