1

我有一个名为费用的表,它包括以下列:

ID, amount_1, amount_2, amount_1_paid, amount_2_paid, total

amount_x_paid是 aTINYINT所以它用于复选框。

我想做的是根据amount_x_paid值(1,0)计算总数

所以让我们说:

amount_1 = 100
amount_2 = 200
amount_1_paid = 1
amount_2_paid = 0

这应该计算

 total = 100

我怎样才能在php中实现这一点?

php 文件将是一项计划任务作业,每天运行一次以计算总数。

这可以解决问题:

SELECT
 FORMAT(
   SUM(
     CASE
       WHEN amount_1_paid = 0 THEN (total + amount_1)
 ELSE (total - amount_1)
       END
    ),
    0
  )
AS total
  FROM
    expenses  
    WHERE ID = '1';

我怎样才能把它放在一个循环中并更新所有记录?

4

3 回答 3

3

amount_根据amount_x_paid列选择字段,可以使用CASE表达式。像这样的东西:

SELECT
  CASE 
    WHEN amount_1_paid = 1 THEN amount_1 
    WHEN amount_2_paid = 1 THEN amount_2  
    ELSE 0
  END AS Amount,
  ...
FROM tablename;

如果您正在寻找这些金额的总和,请在子查询中使用它:

SELECT 
  SUM(Amount) AS Total,
  ...
FROM
(
    SELECT
      CASE 
        WHEN amount_1_paid = 1 THEN amount_1 
        WHEN amount_2_paid = 1 THEN amount_2  
        ELSE 0
      END AS Amount,
      ...
    FROM tablename
) AS sub;
于 2013-04-17T11:23:08.473 回答
2

在你的 php 文件中,你执行一个 mysql 查询:

SELECT SUM(amount_1*amount_1_paid + amount_2*amount_2_paid) AS "total"
FROM expenses;

更新:当您想要按列计算总计时,您可能会考虑不保存它,因为总计完全取决于 amount_1_paid、amount_2_paid、amound_1 和 amound_2。

我的更新查询(不需要循环,会更新整个表)

UPDATE expenses SET total=amount_1*amount_1_paid + amount_2*amount_2_paid;
于 2013-04-17T11:26:20.937 回答
0

这段代码成功了:

            <?php

            include('dbcon.php');

            $query = "SELECT recordno,poso10,poso11,poso12,poso13,poso14,poso15,poso16,poso17,poso18,poso19,poso20,poso21,poso30,poso31,poso32,poso33,poso34,poso41,poso42,poso50,poso51,poso52,poso53,poso60,poso61,poso62,poso63,poso70,poso71,poso10_check,poso11_check,poso12_check,poso13_check,poso14_check,poso15_check,poso16_check,poso17_check,poso18_check,
            poso19_check,poso20_check,poso21_check,poso30_check,poso31_check,poso32_check,poso33_check,poso34_check,poso40_check,
            poso41_check,poso42_check,poso50_check,poso51_check,poso52_check,poso53_check,poso60_check,poso61_check,poso62_check,
            poso63_check,poso70_check,poso71_check,synolo40_42_At_Pa_Bo,
            (if(isnull(poso10),0,poso10) + 
            if(isnull(poso11),0,poso11) + 
            if(isnull(poso12),0,poso12) +
            if(isnull(poso13),0,poso13) + 
            if(isnull(poso14),0,poso14) + 
            if(isnull(poso15),0,poso15) + 
            if(isnull(poso16),0,poso16) + 
            if(isnull(poso17),0,poso17) + 
            if(isnull(poso18),0,poso18) + 
            if(isnull(poso19),0,poso19) + 
            if(isnull(poso20),0,poso20) + 
            if(isnull(poso21),0,poso21) + 
            if(isnull(poso30),0,poso30) + 
            if(isnull(poso31),0,poso31) + 
            if(isnull(poso32),0,poso32) + 
            if(isnull(poso33),0,poso33) + 
            if(isnull(poso34),0,poso34) +  
            if(isnull(poso41),0,poso41) + 
            if(isnull(poso42),0,poso42) + 
            if(isnull(poso50),0,poso50) + 
            if(isnull(poso51),0,poso51) + 
            if(isnull(poso52),0,poso52) + 
            if(isnull(poso53),0,poso53) + 
            if(isnull(poso60),0,poso60) + 
            if(isnull(poso61),0,poso61) + 
            if(isnull(poso62),0,poso62) + 
            if(isnull(poso63),0,poso63) + 
            if(isnull(poso70),0,poso70) + 
            if(isnull(poso71),0,poso71) +
            if(isnull(synolo40_42_At_Pa_Bo),0,synolo40_42_At_Pa_Bo)
            )as synolo

            FROM
            (SELECT
            recordno,poso10_check,poso11_check,poso12_check,poso13_check,poso14_check,poso15_check,poso16_check,poso17_check,poso18_check,
            poso19_check,poso20_check,poso21_check,poso30_check,poso31_check,poso32_check,poso33_check,poso34_check,poso40_check,
            poso41_check,poso42_check,poso50_check,poso51_check,poso52_check,poso53_check,poso60_check,poso61_check,poso62_check,
            poso63_check,poso70_check,poso71_check,
            (CASE WHEN poso10_check = '1' THEN poso10 END) AS poso10,
            (CASE WHEN poso11_check = '1' THEN poso11 END) AS poso11,
            (CASE WHEN poso12_check = '1' THEN poso12 END) AS poso12,
            (CASE WHEN poso13_check = '1' THEN poso13 END) AS poso13,
            (CASE WHEN poso14_check = '1' THEN poso14 END) AS poso14,
            (CASE WHEN poso15_check = '1' THEN poso15 END) AS poso15,
            (CASE WHEN poso16_check = '1' THEN poso16 END) AS poso16,
            (CASE WHEN poso17_check = '1' THEN poso17 END) AS poso17,
            (CASE WHEN poso18_check = '1' THEN poso18 END) AS poso18,
            (CASE WHEN poso19_check = '1' THEN poso19 END) AS poso19,
            (CASE WHEN poso20_check = '1' THEN poso20 END) AS poso20,
            (CASE WHEN poso21_check = '1' THEN poso21 END) AS poso21,
            (CASE WHEN poso30_check = '1' THEN poso30 END) AS poso30,
            (CASE WHEN poso31_check = '1' THEN poso31 END) AS poso31,
            (CASE WHEN poso32_check = '1' THEN poso32 END) AS poso32,
            (CASE WHEN poso33_check = '1' THEN poso33 END) AS poso33,
            (CASE WHEN poso34_check = '1' THEN poso34 END) AS poso34,
            (CASE WHEN poso40_check = '1' THEN synolo40_42_At_Pa_Bo END) AS synolo40_42_At_Pa_Bo,
            (CASE WHEN poso41_check = '1' THEN poso41 END) AS poso41,
            (CASE WHEN poso42_check = '1' THEN poso42 END) AS poso42,
            (CASE WHEN poso50_check = '1' THEN poso50 END) AS poso50,
            (CASE WHEN poso51_check = '1' THEN poso51 END) AS poso51,
            (CASE WHEN poso52_check = '1' THEN poso52 END) AS poso52,
            (CASE WHEN poso53_check = '1' THEN poso53 END) AS poso53,
            (CASE WHEN poso60_check = '1' THEN poso60 END) AS poso60,
            (CASE WHEN poso61_check = '1' THEN poso61 END) AS poso61,
            (CASE WHEN poso62_check = '1' THEN poso62 END) AS poso62,
            (CASE WHEN poso63_check = '1' THEN poso63 END) AS poso63,
            (CASE WHEN poso70_check = '1' THEN poso70 END) AS poso70,
            (CASE WHEN poso71_check = '1' THEN poso71 END) AS poso71

            FROM 010_08_exoda)a;";

            $synolo = mysql_query($query);
            $count=mysql_num_rows($synolo);

            while($row = mysql_fetch_assoc($synolo))
            {
                    $recordno = $row['recordno'];
                    $synolo2 = $row['synolo'];

            mysql_query("UPDATE 010_08_exoda SET synolo_paid='$synolo2' where recordno='$recordno'");  

                }
            ?>
于 2013-05-02T07:57:02.890 回答