duanliang1999 2014-01-26 15:18
浏览 66
已采纳

使用SUM计算多列中的值

I am using this project as a learning curve plus the the option to use it for our pool team.

I am trying to calculate how many wins/ loses and how many games played.

I have a database named weeks and in there i store quite a few columns but now needing to calculate the total number of wins and loses for all weeks i have searched around and found this page which shows a nice example on how to use SUM.

My table is named weeks and it looks like this

id | fs1 | fs2 | fs3 | => fs12

I have been inserting rows for each week so for example

id | fs1 | fs2 | fs3 | => fs12
1     0     1     0        1           =2 wins
2     0     1     1        1           =3 wins

So using the code from the website i linked to above; this is my code i have at present but its not working.

<?php
include 'db_connect.php';

$sql = "SELECT SUM(fs1 + fs2 + fs3 + fs4 + fs5 + fs6 + fs7 + fs8 + fs9 + fs10 + fs11 + fs12) as TotalWins
"
    . "FROM weeks";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result) ;
    $TotalWins =  $row['TotalWins'];

print ($TotalWins); ?>

This code gives me the following error Undefined variable and Query was empty

  • 写回答

1条回答 默认 最新

  • doujiao7483 2014-01-26 15:39
    关注

    In MySQL, sum() only takes one argument and the table name goes in the from clause. You can try this:

    SELECT SUM(fs1 + fs2 + fs3 + fs4 + fs5 + fs6 + fs7 + fs8 + fs9 + fs10 + fs11) as TotalWins
    FROM weeks;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?