在查询中添加两个值的总和

时间:2013-03-20 07:08:51

标签: php mysql

所以我做了一个得到以下结果的查询

 +---------------+---------------+---------+---------
 | payID         | payRate       | hours   | Earnings
 +---------------+---------------+---------+--------
 |  entertainment| 12            |      18 | 216
 |        retail | 10            |      28 | 280
 +---------------+---------------+---------+----------

查询和代码的其他部分如下:

 $query = "SELECT jobId, payRate, SUM(hours) AS 'All_Hours' ,payRate * SUM(hours) AS 'total'
                       FROM users INNER JOIN deposit ON userId = empId
                      WHERE users.email = '" . $_SESSION['email'] ."' 
                      GROUP BY jobId,payRate";



                      $result = mysqli_query($db, $query); //we make the query

                  if (!$result) { //if the query failed
                   echo("<p id = 'greatideadescription'>
                              Error, the query could not be executed: " .
                   mysqli_error($db) . "</p>");
                   mysqli_close($db);}

                if (mysqli_num_rows($result) == 0) { //if no rows returned
                   echo("<tr><td />
                               <td>No Results</td>
                               <td /></tr>");
                   mysqli_close($db); //close the database
                   exit("</table></div></form></div></div>
                               <script>DisplayFooter();</script></body></html>");
                        } 
                       $numRows = mysqli_num_rows($result); //gets number of rows
                       $numFields = mysqli_num_fields($result); //gets number of fields
                       //prints the data in the table
                       PrintTable($result, $numRows, $numFields);

PrintTable函数如下

 function PrintTable($result, $numRows, $numFields) {
  $row = mysqli_fetch_row($result); //fetches the first row
  for ($i = 0; $i < $numRows; $i++) {
  echo("<tr>"); //opens a new row

  for ($j = 0; $j < $numFields; $j++) { //second loop goes through columns
  echo("<td>" . $row[$j] . "</td>"); 
   } //end inner for loop

   $row = mysqli_fetch_row($result); //fetches subsequent rows
   echo("</tr>"); //closes the row
   } //end outer for loop
  } 

我想要的是回应收益的总和,在这种情况下它将是496 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

如果你知道列索引你的总和列,你可以尝试这样的事情:

function PrintTable($result, $numRows, $numFields) {
    $total = 0; // Initialize variable before going through each row
    $row = mysqli_fetch_row($result); //fetches the first row
    for ($i = 0; $i < $numRows; $i++) {
        echo("<tr>"); //opens a new row

        for ($j = 0; $j < $numFields; $j++) { //second loop goes through columns
            echo("<td>" . $row[$j] . "</td>");
            if($j == 3)
              $total += $row[$j];
        } //end inner for loop

        $row = mysqli_fetch_row($result); //fetches subsequent rows
        echo("</tr>"); //closes the row
    } //end outer for loop
}

之后你可以使用你的总数或将它返回到你的主代码中,以便在关闭表格之后回复它或者或者......或者...

更好的方法是进行3次更改:

    在你的sql语句中
  1. payRate * SUM(hours) AS earnings,稍后将使用该名称
  2. 使用mysqli_fetch_assoc获取关联数组,您可以按名称访问列($row['earnings']
  3. 使用foreach-loop作为列,因为您没有可以迭代的索引
  4. 取决于你使用哪种方式...