如何在php中对mysql输出行求和
我有一张看起来像的桌子如何在php中对mysql输出行求和,php,mysql,Php,Mysql,我有一张看起来像的桌子 id user_id white brown green other day month year week 6 54 5 5 2 0 30 11 2015 49 5 48 1 3 1 0 1 12 2015 49 4 2
id user_id white brown green other day month year week
6 54 5 5 2 0 30 11 2015 49
5 48 1 3 1 0 1 12 2015 49
4 2 2 1 2 0 1 12 2015 49
我要做的是选择day==$day的所有记录
但是我想把颜色的值加在一起,这样就不用显示day=1的两个记录了。
在哪里显示一个结果
white = 3
brown = 4
green = 3
and other = 0
我一直在玩我的php脚本几个小时,但我似乎找不到任何适合我的东西
正在工作的代码段
$sql = mysql_query( "SELECT sum(white) AS white, sum(brown) AS brown, sum(green) AS green, sum(other) AS other FROM eggs WHERE day = '1'");
while ($row = mysql_fetch_array($sql)){
?><tr></td>
<td align="center"><?PHP echo $row['white']; ?></td>
<td align="center"><?PHP echo $row['brown']; ?></td>
<td align="center"><?PHP echo $row['green']; ?></td>
<td align="center"><?PHP echo $row['other']; ?></td>
<td align="center"><?PHP echo $total_eggs_today; ?></td>
</tr>
<?PHP
}
$sql=mysql\u query(“选择sum(白色)为白色,sum(棕色)为棕色,sum(绿色)为绿色,sum(其他)为其他,从蛋中选择day='1'”;
while($row=mysql\u fetch\u数组($sql)){
?>
我认为……这个例子对你有帮助
<?php
$sql =
"select
sum(white) white, sum(brown) brown, sum(green) green, sum(other) other
from
table
where
day = 1";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result) {
print_r($row);
}
?>
请显示到目前为止脚本中有哪些内容,以及哪些部分没有按预期工作。它是否需要在php
脚本中?mysql
可以使用sum()
函数为您做到这一点。这里的php代码在哪里?在mysql中执行会更简单更好:选择sum(白色)、sum(棕色)、sum(绿色)、sum(其他)从表中,其中day=@day