문제
테이블에는 다음 정보가 있습니다.
date |vendor|sales|percent| -------------------------- 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 |
다음과 같이 날짜에 따라 별도의 테이블로 정렬하기를 원합니다.
date |vendor|sales|percent| -------------------------- 2009-03-03| 10 |13.50| 1.30 | 2009-03-03| 18 |53.50| 5.30 | 2009-03-03| 21 |23.50| 2.30 | date |vendor|sales|percent| -------------------------- 2009-03-10| 10 |42.50| 4.25 | 2009-03-10| 18 |44.50| 4.45 | 2009-03-10| 21 |32.50| 3.25 |
이 작업을 수행 할 수는 있지만 다음과 같은 각 별도 테이블에 대한 총계를 제공 할 수는 없습니다.
date |vendor|sales|percent| -------------------------- 2009-03-03| 10 |13.50| 1.30 | 2009-03-03| 18 |53.50| 5.30 | 2009-03-03| 21 |23.50| 2.30 | Total Sales for 2009-03-03 = $90.50 Total percent for 2009-03-03 = $8.90 date |vendor|sales|percent| -------------------------- 2009-03-10| 10 |42.50| 4.25 | 2009-03-10| 18 |44.50| 4.45 | 2009-03-10| 21 |32.50| 3.25 | Total Sales for 2009-03-03 = $119.50 Total percent for 2009-03-03 = $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";
?>
해결책
(편집 : 내가 게시했을 때 죄송합니다. 쿼리에서 필요한 것을 보았습니다!)
$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";
?>
다른 팁
SQL
"SELECT date, SUM(sales) FROM Deposits WHERE market = '4' GROUP BY date"
Sum (Sales)의 Istead (판매) 자신의 계산을 사용해야합니다. 나는 그것들을 모델에 넣는 것이 좋습니다.
이것이 도움이 될까요?
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'
따라서 기본적으로 날짜는 원하는 주가 시작되고 정거장을 설명하는 두 날짜보다 작은 곳 ...
또는 ... MySQL의 값에 하루를 추가 할 수 있다고 생각합니다.
'$ yyyy-mm-dd %'와 같은 날짜 인 table_name에서 합 (판매)을 선택하십시오.
사용하여 해당 합계를 바로 얻을 수 있습니다 롤업으로 그룹.
제휴하지 않습니다 StackOverflow