如何在php中将结果分组为子组
大家好。当我试图学习一些PHP和mySQL时,我遇到了一个问题,我在解决这个问题时遇到了一些困难。我需要一个PHP脚本,查询国家,书籍,书籍价格mySQL数据库。这是我的桌子:如何在php中将结果分组为子组,php,mysql,Php,Mysql,大家好。当我试图学习一些PHP和mySQL时,我遇到了一个问题,我在解决这个问题时遇到了一些困难。我需要一个PHP脚本,查询国家,书籍,书籍价格mySQL数据库。这是我的桌子: +----+-------+----------+------------------------+ | id |Country|Books | Book_price | +----+-------+----------+------------------------+ | 1 |
+----+-------+----------+------------------------+
| id |Country|Books | Book_price |
+----+-------+----------+------------------------+
| 1 | USA | Zorro | 10 |
| 2 | USA | Zorro | 20 |
| 3 | USA | Zorro | 50 |
| 4 | USA | Leon | 200 |
| 5 | USA | Leon | 240 |
| 6 | ITALY| Tarzan | 70 |
| 7 | ITALY| Tarzan | 30 |
| 8 | ITALY| Tarzan | 100 |
| 9 | ITALY| Cobra | 300 |
| 10 | ITALY| Cobra | 320 |
| 11 | ITALY| Cobra | 350 |
+----+------+-----------+------------------------+
我想根据国家、书籍、总书籍、总国家和总发电量来组织结果,这是所有总国家和结果的总和,如下所示:
+----------------------------------------------------+
| USA |
+----------------------------------------------------+
| Zorro 10 |
| 20 |
| 50 |
+----------------------------------------------------+
| Total Zorro: 80 |
+----------------------------------------------------+
| Leon 200 |
| 240 |
+----------------------------------------------------+
| Total Leon:440 |
+----------------------------------------------------+
|Total USA: 520 |
+----------------------------------------------------+
|ITALY |
+----------------------------------------------------+
| Tarzan 70 |
| 30 |
| 100 |
+----------------------------------------------------+
| Total Tarzan:200 |
+----------------------------------------------------+
| Cobra 300 |
| 320 |
| 350 |
+----------------------------------------------------+
| Total Cobra: 970 |
+----------------------------------------------------+
|Total ITALY: 1170 |
+----------------------------------------------------+
|TOTAL GEN: 1690 |
+----------------------------------------------------+
谢谢试试:
SELECT country, books, SUM(price)
FROM sales
GROUP BY country, books WITH ROLLUP;
books为空的行将返回每个国家的总计
国家和账簿为空的行返回整个查询的总计使用保存最后一个国家/账簿及其小计的其他变量:
$last = array('Country' => null, 'Books' => null);
$subtotals = array('Country' => 0, 'Books' => 0);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
if ($row['Books'] !== $last['Books']) {
if (!is_null($last['Books'])) {
echo '<tr><td colspan="2">Total '.$last['Books'].': '.$subtotals['Books'].'</td></tr>';
}
$last['Books'] = $row['Books'];
$subtotals['Books'] = $row['Book_price'];
} else {
$subtotals['Books'] += $row['Book_price'];
}
if ($row['Country'] !== $last['Country'])) {
if (!is_null($last['Country'])) {
echo '<tr><td colspan="2">Total '.$last['Country'].': '.$subtotals['Country'].'</td></tr>';
}
echo '<tr><th colspan="2">'.$row['Country'].'</th></tr>';
$last['Country'] = $row['Country'];
$subtotals['Country'] = $row['Book_price'];
} else {
$subtotals['Country'] += $row['Book_price'];
}
echo '<tr><td>'.$row['Books'].'</td><td>'.$row['Book_price'].'</td></tr>';
}
echo '</table>';
看看这个
<?php
include ("config.php");
function render($price3) {
$output = "<td align='right'>".number_format($price3->book_price, 0, ',', '.')."</td>";
$output .= "</tr>";
return $output;
}
echo "<table border='1'>";
echo "<tr>
<th>books</th>
<th>price</th>
</tr>";
$result = mysql_query("select id, country, books, book_price from test") or die(mysql_error());
$set = array();
while ($record = mysql_fetch_object($result)) {
$set[$record->country][$record->books][] = $record;
}
$sum_country = 0;
foreach ($set as $country => $price1) {
echo "<tr>
<td align='center'>{$country}</font></td>
<td></td>
</tr>";
foreach ($price1 as $books => $price2 ) {
echo "<tr>
<td>{$books}</td>";
foreach ($price2 as $price3) {
echo render($price3);
}
$sum_books = 0;
foreach($price2 as $grbooks){
$sum_books += $grbooks->book_price;
}
echo "<tr><td>Total {$books}</td>
<td align='right'>".number_format($sum_books, 0, ',', '.')."</td>
</tr>";
}
$sum_country += $sum_books;
echo "<tr<td>Total {$country}</td>
<td align='right'>".number_format($sum_country, 0, ',', '.')."</td>
</tr>";
}
echo "</table>";
<?php
include ("config.php");
function render($price3) {
$output = "<td align='right'>".number_format($price3->book_price, 0, ',', '.')."</td>";
$output .= "</tr>";
return $output;
}
echo "<table border='1'>";
echo "<tr>
<th>books</th>
<th>price</th>
</tr>";
$result = mysql_query("select id, country, books, book_price from test") or die(mysql_error());
$set = array();
while ($record = mysql_fetch_object($result)) {
$set[$record->country][$record->books][] = $record;
}
$sum_country = 0;
foreach ($set as $country => $price1) {
echo "<tr>
<td align='center'>{$country}</font></td>
<td></td>
</tr>";
foreach ($price1 as $books => $price2 ) {
echo "<tr>
<td>{$books}</td>";
foreach ($price2 as $price3) {
echo render($price3);
}
$sum_books = 0;
foreach($price2 as $grbooks){
$sum_books += $grbooks->book_price;
}
echo "<tr><td>Total {$books}</td>
<td align='right'>".number_format($sum_books, 0, ',', '.')."</td>
</tr>";
}
$sum_country += $sum_books;
echo "<tr<td>Total {$country}</td>
<td align='right'>".number_format($sum_country, 0, ',', '.')."</td>
</tr>";
}
echo "</table>";