大家好,
$sql = " SELECT prd.id as id,prd.weight as wgh,
prd.srpid as srno,prd.dollarval as dolval,catid.id
FROM productname as prd
INNER JOIN catid as caid ON prd.catid = caid.id
Where 1=1
ORDER BY sid ASC "
$sqlquery = mysql_query($sql);
其中id为主要,自动递增
以上价值低于:
id | wgh | srno| dolval
1 1.05 1 155
2 1.00 3 300
3 0.67 2 170
4 0.01 3 140
5 0.20 2 50
6 0.35 1 300
7 2.04 4 190
8 3.0 5 200
做完PHP逻辑后,我得到以下结果作为输出(显示):
echo"<table width=\"100%\" border=\"1\">
<tr>
<td align=\"center\" valign=\"top\"><strong>Sr.No</strong></td>
<td align=\"center\" valign=\"top\"><strong>Cur Wg</strong></td>
<td align=\"center\" valign=\"top\"><strong>Dollar</strong></td>
</tr>";
$srno = 1;
$pid = null;
$sumnew = 0;
$sumdollor = 0;
while($row = mysql_fetch_array($sqlquery)){
$rowval = $row['srno']; // **** result is on this value.....
if ($rowval == $pid || $pid === null){
echo "<tr>";
if($pid != $rowpkts){
echo'<td align="center" valign="top">'.$srno++.'</td>';
}else{
echo'<td align="center" valign="top"></td>';
}
echo'<td align="center" valign="top">'.number_format($row["wgh"],2,'.','').'</td>';
echo'<td align="center" valign="top">'.round($row["dolval"],0).'</td>';
echo "</tr>";
$pid = $rowval;
$sumnew += number_format($row["wgh"],2,'.','');
$sumdollor+= round($row["dolval"],0);
}else{
echo "<tr>
<td></td>
<td align=\"center\" valign=\"top\"><strong>".number_format($sumnew,2,'.','')."</strong></td>
<td align=\"center\" valign=\"top\"><strong>".$sumdollor."</strong></td>
echo "</tr>";
<tr><td colspan='3' height='18px'></td></tr>";
echo "<tr>";
if($pid != $rowpkts){
echo'<td align="center" valign="top">'.$srno++.'</td>';
}else{
echo'<td align="center" valign="top"></td>';
}
echo'<td align="center" valign="top">'.number_format($row["wgh"],2,'.','').'</td>';
echo'<td align="center" valign="top">'.round($row["dolval"],0).'</td>';
echo "</tr>";
$sumnew = number_format($row["wgh"],2,'.','');
$sumdollor = round($row["dolval"],0);
$pid = $rowval;
}
}
echo "<tr>
<td></td>
<td align=\"center\" valign=\"top\"><strong>".number_format($sumnew,2,'.','')."</strong></td>
<td align=\"center\" valign=\"top\"><strong>".$sumdollor."</strong></td>
echo "</tr>";
echo"</table>";
从上面的PHP代码我得到以下结果:
Sr.No Wgh Dollar
1 1.05 155
0.35 300
-----------------------
455 //( 155+300=455)
------------------------
2 0.67 170
0.20 50
------------------------
220
------------------------
3 1.00 300
0.01 140
------------------------
440
------------------------
4 2.04 190
------------------------
190
------------------------
5 3.00 200
------------------------
200
但是我需要在第一次美元的总和中显示上述结果,那么总和的更高的美元(即445,440,220 ......等)应该先来,然后等等。 .. 下面需要
Sr.No Wgh Dollar
1 1.05 155
0.35 300
-----------------------
455 //( 155+300=455)
-----------------------
2 1.00 300
0.01 140
-----------------------
440
-----------------------
3 0.67 170
0.20 50
------------------------
220
------------------------
4 3.00 200
------------------------
200
------------------------
5 2.04 190
------------------------
190
我如何获得以上结果请帮助我...如果有任何其他方式让我知道将尝试更改上述格式但需要结果...
编辑:如果某人有 groupby 的解决方案..这对我有帮助..但我没有使用分组,因为我需要显示所有8个值... 感谢
答案 0 :(得分:1)
您可以使用此SQL,它会生成一个接近您想要生成的输出(和顺序):
SELECT prd.srpid as srno,
prd.weight as wgh,
prd.dollarval as dolval,
s.dollarsum
FROM productname as prd
INNER JOIN catid as caid ON prd.catid = caid.id
INNER JOIN (select srpid,
sum(dollarval) dollarsum
from productname
group by srpid) s ON s.srpid = prd.srpid
ORDER BY s.dollarsum DESC,
prd.srpid DESC,
prd.weight DESC
这是SQL fiddle。
生成的输出是:
+-------+--------+-----------+-----------+
| srpid | weight | dollarval | dollarsum |
+-------+--------+-----------+-----------+
| 1 | 1.05 | 155 | 455 |
| 1 | 0.35 | 300 | 455 |
| 3 | 1 | 300 | 440 |
| 3 | 0.01 | 140 | 440 |
| 2 | 0.67 | 170 | 220 |
| 2 | 0.2 | 50 | 220 |
| 5 | 3 | 200 | 200 |
| 4 | 2.04 | 190 | 190 |
+-------+--------+-----------+-----------+
因此,在PHP中,您需要获取最后一列中返回的值,并在每次进入新部分时将其作为单独的“小计”行输出。
我不会那么远,但您甚至可以让SQL将这些小计作为行生成:
SELECT prd.srpid as srno,
prd.weight as wgh,
prd.dollarval as dolval,
s.dollarsum
FROM productname as prd
INNER JOIN catid as caid ON prd.catid = caid.id
INNER JOIN (select srpid,
sum(dollarval) dollarsum
from productname
group by srpid) s ON s.srpid = prd.srpid
UNION
select srpid,
null,
'sum:',
sum(dollarval) dollarsum
from productname
group by srpid
ORDER BY 4 DESC,
1 DESC,
2 DESC
输出:
+-------+--------+-----------+-----------+
| srpid | weight | dollarval | dollarsum |
+-------+--------+-----------+-----------+
| 1 | 1.05 | 155 | 455 |
| 1 | 0.35 | 300 | 455 |
| 1 | | sum: | 455 |
| 3 | 1 | 300 | 440 |
| 3 | 0.01 | 140 | 440 |
| 3 | | sum: | 440 |
| 2 | 0.67 | 170 | 220 |
| 2 | 0.2 | 50 | 220 |
| 2 | | sum: | 220 |
| 5 | 3 | 200 | 200 |
| 5 | | sum: | 200 |
| 4 | 2.04 | 190 | 190 |
| 4 | | sum: | 190 |
+-------+--------+-----------+-----------+