数据表:
货号 颜色 单价 数量
001 黑 10 10
001 黑 12 5
001 白 15 15
001 白 20 2
002 黑 5 5
002 黑 6 10
002 白 8 15
002 白 8 40
.还有很多类似数据
要实现以下目标:
货号 颜色 数量 小计总价
001 黑 22 160
白 35 265
小计:57 425
002 黑 11 85
白 16 440
小计: 27 525
.还有很多类似汇总总计:数量200 总价10000
(⊙_⊙),我真的想不出来。查询实现这些数据不难,但是要这种显示方法就不知道了。谢谢大家回复!
货号 颜色 单价 数量
001 黑 10 10
001 黑 12 5
001 白 15 15
001 白 20 2
002 黑 5 5
002 黑 6 10
002 白 8 15
002 白 8 40
.还有很多类似数据
要实现以下目标:
货号 颜色 数量 小计总价
001 黑 22 160
白 35 265
小计:57 425
002 黑 11 85
白 16 440
小计: 27 525
.还有很多类似汇总总计:数量200 总价10000
(⊙_⊙),我真的想不出来。查询实现这些数据不难,但是要这种显示方法就不知道了。谢谢大家回复!
<table width="600" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="000000">
<tr bgcolor="#FF9900">
<th height="30">货号</th>
<th>颜色</th>
<th>数量</th>
<th>总价</th>
</tr>
<?php
$result = array(
array( 'id' => '001', 'color' => '黑', 'price' => 10, 'count' => 10 ),
array( 'id' => '001', 'color' => '黑', 'price' => 12, 'count' => 5 ),
array( 'id' => '001', 'color' => '白', 'price' => 15, 'count' => 15 ),
array( 'id' => '001', 'color' => '白', 'price' => 20, 'count' => 2 ),
array( 'id' => '002', 'color' => '黑', 'price' => 5, 'count' => 5 ),
array( 'id' => '002', 'color' => '黑', 'price' => 6, 'count' => 10 ),
array( 'id' => '002', 'color' => '白', 'price' => 8, 'count' => 15 ),
array( 'id' => '002', 'color' => '白', 'price' => 8, 'count' => 40 )
);
$data = $zj = array();
foreach ( $result as $row ){
extract($row);
$data[$id][$color]['count'] += $count;
$data[$id][$color]['total'] += $count * $price;
}
foreach ( $data as $key => $val ){
$td = '<td rowspan="' . count( $val ) . '">' . $key . "</td>\r\n";
$xj = array();
foreach ( $val as $k => $v) {
$xj[0] += $v['count'];
$xj[1] += $v['total'];
?>
<tr align="center" bgcolor="#FFFFFF">
<?php echo $td; ?>
<td height="24"><?php echo $k; ?></td>
<td><?php echo $v['count']; ?></td>
<td><?php echo $v['total']; ?></td>
</tr>
<?php
$td = '';
}
$zj[0] += $xj[0];
$zj[1] += $xj[1];
?>
<tr align="center" bgcolor="#FFFFCC">
<th height="30" colspan="2">小计:</th>
<th><?php echo $xj[0]; ?></th>
<th><?php echo $xj[1]; ?></th>
</tr>
<?php
}
?>
<tr align="center" bgcolor="#FF9900">
<th height="30" colspan="2">总计:</th>
<th><?php echo $zj[0]; ?></th>
<th><?php echo $zj[1]; ?></th>
</tr>
</table>这样测试下!
`货号` smallint(3) NOT NULL,
`颜色` varchar(4) NOT NULL,
`单价` smallint(3) NOT NULL,
`数量` smallint(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
$conn = mysql_connect('localhost','root','root');
mysql_select_db('mmtest',$conn);
mysql_set_charset('gbk');$create_sql = 'drop view if exists t1;drop view if exists t2;create temporary table t1 as select 货号,颜色,单价,数量,sum(数量) as 小计量,sum(单价*数量) as 小计价 from aa group by 货号,颜色 order by 货号 asc,颜色 desc;
create temporary table t2 as select 货号,sum(小计量) as 总量,sum(小计价) as 总计 from t group by 货号;';
mysql_query($create_sql);
$sql = "select m.*,n.* from t1 as m left join t2 as n on m.货号=n.货号 ;";$query = mysql_query($sql);
$tmp = '';
echo "货号\t颜色\t数量\t小计<br />" ;
while($re = mysql_fetch_assoc($query)){
echo $re["货号"]."\t ".$re["颜色"]."\t ".$re["小计量"]."\t ".$re["小计价"]."<br />";
if($tmp == $re['货号']){
echo " \t 小计\t".$re["总量"]."\t ".$re["总计"]."<br />";
}
$tmp = $re['货号'];
}
mysql_close($conn);输出:
货号 颜色 数量 小计
1 黑 15 160
1 白 17 265
小计 32 425
2 黑 15 85
2 白 55 440
小计 70 525
你的代码我更正如下:以下可以直接在mysql内执行,生成视图。
$create_sql = 'create view t1 as select 货号,颜色,单价,数量,sum(数量) as 小计量,sum(单价*数量) as 小计价 from aa group by 货号,颜色 order by 货号 asc,颜色 desc;
create view t2 as select 货号,sum(小计量) as 总量,sum(小计价) as 总计 from t1 group by 货号;';
mysql_query($create_sql);
$sql = "select t1.*,t2.* from t1 left join t2 on t1.货号=t2.货号 ;";
你的程序总得要在php里运行吧 嘿嘿 我给个修正版的,颜色个数不限。header("Content-Type: text/html;charset=gbk");
$conn = mysql_connect('localhost','root','root');
mysql_select_db('mmtest',$conn);
mysql_set_charset('gbk');$create_sql = 'drop view if exists t1;drop view if exists t2;create temporary table t1 as select 货号,颜色,单价,数量,sum(数量) as 小计量,sum(单价*数量) as 小计价 from aa group by 货号,颜色 order by 货号 asc,颜色 desc;
create temporary table t2 as select 货号,sum(小计量) as 总量,sum(小计价) as 总计 from t group by 货号;';
mysql_query($create_sql);
$sql = "select m.*,n.* from t1 as m left join t2 as n on m.货号=n.货号 ;";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
$i = 0;
$tmp = array();echo "货号\t颜色\t数量\t小计<br />" ;
while($re = mysql_fetch_assoc($query)){
if((isset($tmp['货号']) && $tmp['货号'] == $re['货号']) || count($tmp)==0){
echo $re["货号"]."\t ".$re["颜色"]."\t ".$re["小计量"]."\t ".$re["小计价"]."<br />";
}else{
echo " \t 小计\t".$tmp["总量"]."\t ".$tmp["总计"]."<br />";
echo $re["货号"]."\t ".$re["颜色"]."\t ".$re["小计量"]."\t ".$re["小计价"]."<br />";
}
$tmp = $re ;
$i++ ;
if($i == $count){
echo " \t 小计\t".$re["总量"]."\t ".$re["总计"]."<br />";
}
}
mysql_close($conn);
输出结果:
货号 颜色 数量 小计
1 黑 15 160
1 白 17 265
小计 32 425
2 灰 50 765
2 黑 15 85
2 白 55 440
小计 120 1290