在SQL中创建排名系统,变量问题

时间:2017-10-09 14:31:30

标签: mysql variables scoring

我的查询创建排名系统时遇到了一些麻烦。 我的查询很好,但我使用了一些变量,结果不一致。

第一个案例陈述是好的,但不是第二个。我是否有可能在没有使用变量的情况下进行第二次计算?

        CASE
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 0 AND 100 AND DATEDIFF(NOW(),MAX(pso.date_add))        BETWEEN 0 AND 50        THEN @scoring := ROUND(50 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 0 AND 100 AND DATEDIFF(NOW(),MAX(pso.date_add))        BETWEEN 51 AND 150      THEN @scoring := ROUND(20 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 0 AND 100 AND DATEDIFF(NOW(),MAX(pso.date_add))        BETWEEN 151 AND 364     THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 0 AND 100 AND DATEDIFF(NOW(),MAX(pso.date_add))        >= 365                  THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/20 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 101 AND 499 AND DATEDIFF(NOW(),MAX(pso.date_add))      BETWEEN 0 AND 50        THEN @scoring := ROUND(50 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/3 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 101 AND 499 AND DATEDIFF(NOW(),MAX(pso.date_add))      BETWEEN 51 AND 150      THEN @scoring := ROUND(20 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/3 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 101 AND 499 AND DATEDIFF(NOW(),MAX(pso.date_add))      BETWEEN 151 AND 364     THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/3 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) BETWEEN 101 AND 499 AND DATEDIFF(NOW(),MAX(pso.date_add))      >= 365                  THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/20 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) >= 500 AND DATEDIFF(NOW(),MAX(pso.date_add))                   BETWEEN 0 AND 50        THEN @scoring := ROUND(50 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/5 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) >= 500 AND DATEDIFF(NOW(),MAX(pso.date_add))                   BETWEEN 51 AND 150      THEN @scoring := ROUND(20 + SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/5 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) >= 500 AND DATEDIFF(NOW(),MAX(pso.date_add))                   BETWEEN 151 AND 364     THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/5 + COUNT(pso.id_order)*10,0)
            WHEN SUM(pso.total_products_wt)/COUNT(pso.total_products_wt) >= 500 AND DATEDIFF(NOW(),MAX(pso.date_add))                   >= 365                  THEN @scoring := ROUND(SUM(pso.total_products_wt)/COUNT(pso.total_products_wt)/20 + COUNT(pso.id_order)*10,0)
        END AS 'Scoring',
        CASE
            WHEN @scoring >= 200 THEN '5/5'
            WHEN @scoring BETWEEN 150 AND 199 THEN '4/5'
            WHEN @scoring BETWEEN 100 AND 149 THEN '3/5'
            WHEN @scoring BETWEEN 50 AND 99 THEN '2/5'
            WHEN @scoring <= 49 THEN '1/5'
        END AS 'Rank'

1 个答案:

答案 0 :(得分:1)

来自文档https://dev.mysql.com/doc/refman/5.5/en/user-variables.html

  
    

但是,涉及用户变量的表达式的评估顺序是未定义的。

  

所以在你的第二个表达式中,你可以在第一个表达式(正如你所期望的)修改之后读取变量Infinity,或者恰好 之前。

解决方案:

@scoring