0

表有以下信息:

日期|供应商|销售额|百分比|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-10| 10 |42.50| 4.25 |
2009-03-03| 21 |23.50| 2.30 |
2009-03-10| 21 |32.50| 3.25 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-10| 18 |44.50| 4.45 |

我希望它根据日期分类到单独的表格中,如下所示:

日期|供应商|销售额|百分比|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-03| 21 |23.50| 2.30 |

日期|供应商|销售额|百分比|
--------------------------
2009-03-10| 10 |42.50| 4.25 |
2009-03-10| 18 |44.50| 4.45 |
2009-03-10| 21 |32.50| 3.25 |

我可以完成这项工作,但我无法为每个单独的表格提供总计,例如:

日期|供应商|销售额|百分比|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-03| 21 |23.50| 2.30 |
2009 年 3 月 3 日的总销售额 = 90.50 美元
2009 年 3 月 3 日的总百分比 = 8.90 美元

日期|供应商|销售额|百分比|
--------------------------
2009-03-10| 10 |42.50| 4.25 |
2009-03-10| 18 |44.50| 4.45 |
2009-03-10| 21 |32.50| 3.25 |
2009 年 3 月 3 日的总销售额 = 119.50 美元
2009 年 3 月 3 日的总百分比 = 11.95 美元

我可以获得所有表格的总数,但不能获得单个表格的总数。这是我的代码:

    <?php
$con = mysql_connect("localhost", $dbUser, $dbPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("beans", $con);

$result = mysql_query("SELECT * FROM Deposits WHERE market = '4' ORDER BY eventdate, vendor ASC") or die(mysql_error());

$dateChk = 0;

while($row = mysql_fetch_array($result))
{ 
    $date = $row["eventdate"];
    $liclass = $row["vendor"];
    $url = $row["trxid"];
    $amountdep = $row["amount"];
    $depcheck = $row["checkno"];
       $deposit_Total = $deposit_Total + $amountdep;
       $deposit_3Total = $deposit_3Total + $depcheck;
       $deposit_3 = $amountdep / 100;
       $dep_percent = $deposit_3 * 3;
       $depper_Total = $depper_Total + $dep_percent;
       $week = date("W", db_date_to_timestamp($date));
       $year = date("Y", db_date_to_timestamp($date));        


If($dateChk != $week)
{     

        echo "<table class=\"adverts\" width=\%100\" cellpadding=\"4\">\n";
        echo "<tr><th>Date</th><th>Vendor</th><th>Total Sales</th><th>3% Due</th><th>Week</th></tr>\n";
        echo "<tr>";  
        echo "<td>$date</td>\n";     
        echo "<td>$liclass</td>\n";           
        echo "<td>$ $amountdep</td>\n";               
        echo "<td>$ $depcheck</td>\n";
        echo "<td>$week</td>\n";      
        echo "</tr>";

}
else 
{
        echo "<tr>";  
        echo "<td>$date</td>\n";   
        echo "<td>$liclass</td>\n";                 
        echo "<td>$ $amountdep</td>\n";               
        echo "<td>$ $depcheck</td>\n";
        echo "<td>$week</td>\n";             
        echo "</tr>";

}
$dateChk = $week;
} 
        echo "</table>\n";
        echo "<p><b>Total reported Market Sales are $ " . $deposit_Total . "</b></p>\n";
        echo "<p><b>3 percent of Total reported Market Sales are $  " . $deposit_3Total . "</b></p>\n";
?>
4

4 回答 4

1

(编辑:抱歉,当我发布时我看到您在查询中需要它!)

$dateChk = 0;
$firstRow = 1;

while($row = mysql_fetch_array($result))
{ 
    $date = $row["eventdate"];
    $liclass = $row["vendor"];
    $url = $row["trxid"];
    $amountdep = $row["amount"];
    $depcheck = $row["checkno"];
       $deposit_Total = $deposit_Total + $amountdep;
       $deposit_3Total = $deposit_3Total + $depcheck;
       $deposit_3 = $amountdep / 100;
       $dep_percent = $deposit_3 * 3;
       $depper_Total = $depper_Total + $dep_percent;
       $week = date("W", db_date_to_timestamp($date));
       $year = date("Y", db_date_to_timestamp($date));        

If($dateChk != $week)
{         
    if($firstRow == 0)          
    {
        echo "</table>\n";
        echo "<p><b>Total reported Market Sales are $ " . $deposit_week_Total . "</b></p>\n";            
        echo "<p><b>3 percent of Total reported Market Sales are $  " . $deposit_week_3Total . "</b></p>\n";
        $deposit_week_Total = 0;            
        $deposit_week_3Total = 0;

    }else 
    {
        $firstRow = 0;
    }

        echo "<table class=\"adverts\" width=\%100\" cellpadding=\"4\">\n";
        echo "<tr><th>Date</th><th>Vendor</th><th>Total Sales</th><th>3% Due</th><th>Week</th></tr>\n";
        echo "<tr>";  
        echo "<td>$date</td>\n";     
        echo "<td>$liclass</td>\n";           
        echo "<td>$ $amountdep</td>\n";               
        echo "<td>$ $depcheck</td>\n";
        echo "<td>$week</td>\n";      
        echo "</tr>";

}
else 
{
        echo "<tr>";  
        echo "<td>$date</td>\n";   
        echo "<td>$liclass</td>\n";                 
        echo "<td>$ $amountdep</td>\n";               
        echo "<td>$ $depcheck</td>\n";
        echo "<td>$week</td>\n";             
        echo "</tr>";

}
$dateChk = $week;
$deposit_week_Total = $deposit_week_Total + $amountdep;          
$deposit_week_3Total = $deposit_week_3Total + $depcheck;
} 
        echo "</table>\n";
        echo "<p><b>Total reported Market Sales are $ " . $deposit_Total . "</b></p>\n";
        echo "<p><b>3 percent of Total reported Market Sales are $  " . $deposit_3Total . "</b></p>\n";
?>
于 2009-09-10T19:17:32.393 回答
0

SQL

"SELECT date, SUM(sales) FROM Deposits WHERE market = '4' GROUP BY date"

您必须使用自己的计算来代替 sum(sales)。我建议将它们放入模型中。

于 2009-09-10T19:17:39.617 回答
0

这会有帮助吗?

http://www.plus2net.com/sql_tutorial/sql_sum.php

SELECT SUM (sales) FROM `table_name` WHERE `date` = '$date_you_want'

从我下面的评论:

SELECT SUM (sales) FROM `table_name` WHERE `date` <= '$start_date_of_week' AND `date` >= '$end_date_of_week'

所以基本上 WHERE 日期小于和大于描述您想要的一周的开始和停止的两个日期......

或者...我认为您也可以将天数添加到 mysql 中的值中

SELECT SUM (sales) FROM table_name WHERE date LIKE '$yyyy-mm-dd %' ...DATE_ADD 代码用于添加天数.. 或 INTERVAL。

于 2009-09-10T19:22:52.263 回答
0

您可以使用GROUP BY WITH ROLLUP直接获得这些总和。

于 2009-09-11T00:14:55.047 回答