Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql的For循环替代方案_Mysql_Sql_Database_Sql Function - Fatal编程技术网

mysql的For循环替代方案

mysql的For循环替代方案,mysql,sql,database,sql-function,Mysql,Sql,Database,Sql Function,假设我有一个税收生成模块,在该模块上,特定金额范围的税收是动态的。该范围存储在如下表中: +---------------+---------------+-----------+ | range_from | range_to | rate | +---------------+---------------+-----------+ | 0 1000 25 | | 1001

假设我有一个税收生成模块,在该模块上,特定金额范围的税收是动态的。该范围存储在如下表中:

+---------------+---------------+-----------+
|   range_from  |   range_to    |   rate    |
+---------------+---------------+-----------+   
|        0          1000            25      |
|       1001        5000            30      |
|       5001        8000            40      |
|       8000          -             50      |
+-------------------------------------------+       
此范围表可以有任意数量的行

那么,如何在MySQL上使用函数实现上表中的税额呢

如果我提供20000,应该是这样计算的

  1000 * 0.25 + 4000 * 0.30 + 3000 * 0.40 + 12000 * 0.50
我试着用

  set @remaining_amount = @provided_amount;
  for x in (select * from range_table)
  loop
      if(@provided_amount > x.range_to) 
        set @tax_amt+=(x.range_to-x.range_from)* x.rate/100;
        set @remaining_amt-= x.range_to-x.range_from; 
      end if;
      if(@provided_amount > x.range_from and x.range_to = '-') 
        set @tax_amt+=(@remaining_amount)* x.rate/100;
      end if;
  end loop;
但我没有发现“for循环”在MySQL上工作

有什么建议/帮助吗?

试试这个:

SUM(IF(x < range_from, 0, 
        IF(x < range_to OR range_to IS NULL, (x - range_from)*rate*0.01, 
                                             (range_to - range_from)*rate*0.01))
SUM(如果(x
目前为5.1.51,但假设为5+,因为必须部署该系统的其他系统的版本可能较低。请检查我的答案。也许这就是您要找的。这里的x是什么?我不理解您的查询。