如何停止循环并添加值

如何停止循环并添加值

我有下表:

id   tax    rate   quantity
 1   20     400       5
 2   20     566       2
 3   5      200       4

以下是我的预期产出:
 taxableamount   taxamount      total
    3132.00        626.40      3758.40
     800.00         40.00       840.00

让我在这里解释一下我的尝试:
如果每一行有相同的税值,我从每一行数据中计算出税额(rate × quantity),然后计算出税额(amount × tax ÷ 100),并将结果显示在一行中,即使它们来自多行。但如果行的税值不相同,则将以单独的行显示。
$query = "SELECT SUM(
          CASE WHEN tax=tax
          THEN rate*quantity ELSE 0 END) AS taxableamount,
          SUM(CASE WHEN tax=tax
          THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS tax,
          SUM(CASE WHEN tax=tax
          THEN (rate*quantity)*tax/100 ELSE 0 END) AS taxamount,
          SUM(CASE WHEN tax=tax
          THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS total
          FROM pricing group by tax";
while($row = show($query)):
   echo '<td>'.$row->taxableamount.'</td>';
   echo '<td>'.$row->taxamount.'</td>';
   echo '<td>'.$row->total.'</td>';
endwhile;

上面的代码给了我错误和意外的结果。所以看看我的预期产出,我该怎么做。请帮忙。

最佳答案

你应该删除你的案件陈述。正如我在评论中所说,征税总是等同于征税,因为你是在和同一行做比较。我加了税栏,所以你知道每种税的金额:

SELECT
    SUM(rate * quantity) AS taxableamount,
    SUM(rate * quantity * tax / 100) AS taxamount,
    SUM(rate * quantity * (1 + tax) / 100) AS total,
    tax
FROM
    pricing
GROUP BY
    tax

关于php - 如何停止循环并添加值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45802673/

10-10 22:08