1

这是我的代码,但它显示了两个不同的值总和。我有每行的总数,它有 2 个值 100 和 200 。它分别显示 100 和 200。但我想要 100 和 200 的总和。总和= 300 我该怎么做?

$result2 = mysql_query("select * from price  where dom='$cat'",$db);
while ($myrow2 = mysql_fetch_array($result2))
{
    echo $myrow2["totalwithper"];
}

这是我的表结构

CREATE TABLE `price` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `dom` varchar(255) COLLATE utf8_bin NOT NULL,
  `etiket` varchar(255) COLLATE utf8_bin NOT NULL,
  `pricestandart` int(5) NOT NULL,
  `number` int(5) NOT NULL,
  `totalunper` int(5) NOT NULL,
  `discount` int(5) NOT NULL,
  `totalwithper` int(5) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=20 ;

--
-- Dumping data for table `price`
--

INSERT INTO `price` (`id`, `dom`, `etiket`, `pricestandart`, `number`, `totalunper`, `discount`, `totalwithper`) VALUES
(18, 'Alten Group', 'flayer', 100, 1, 100, 10, 90),
(19, 'Alten Group', 'logo', 100, 2, 200, 15, 170);
4

4 回答 4

1

在过去的几天里,我一直在尝试使该array_sum()功能正常工作,它会打印出每个数字,但不会将它们加在一起。但是有了这段代码,它终于对我有用了。

 $sum=0;
 $result2 = mysql_query ("select totalwithper from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
 {
    $sum=$sum+ $myrow2["totalwithper"];
 }

 echo "sum : $sum";
于 2014-01-23T19:55:46.993 回答
0
SELECT Sum(totalwithper) FROM price  WHERE ...

看:

于 2012-08-08T12:03:55.390 回答
0

试试这个:

  $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
  $myrow2= mysql_fetch_array($result2);
  do{
     echo $myrow2["totalwithper"];
    }
    while($myrow2=mysql_fetch_array($result2));

对您的代码进行小幅改写:

 $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
        {
         echo $myrow2["totalwithper"];
        }

循环求和

 $sum=0;
 $result2 = mysql_query ("select totalwithper from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
        {
         $sum=$sum+ $myrow2["totalwithper"];
        }

 echo "sum : $sum";

在 $sum 你会得到总和。

于 2012-08-08T12:05:12.567 回答
0

采用

$result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
于 2012-08-08T12:05:59.383 回答