0

我试图在数据库中获取几行选定的 Money Column 行的总和。

sql如下。

$sql_total_money = "SELECT SUM(Money) as TotalMoney FROM accounts WHERE Program='PSC' BranchId='13' and ExamYear='2013'";
$result_total_money=mysql_query($sql_total_money,$link)or die($sql_total_money."<br/><br/>".mysql_error());
$row_total_money=mysql_fetch_array($result_total_money);

由于 die 功能,这会出现如下错误。

SELECT SUM(Money) as TotalMoney FROM accounts WHERE Program='PSC' BranchId='10' and ExamYear='2013'

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BranchId='10' and ExamYear='2013'' at line 1

我能做什么,解决方案是什么?

4

2 回答 2

3

您错过了ANDafter Program='PSC'

SELECT SUM(Money) as TotalMoney
FROM   accounts
WHERE  Program='PSC'
       AND BranchId='10'
       AND ExamYear='2013'
于 2013-07-07T12:07:32.993 回答
0

您需要使用条件运算符来组合您的条件,AND/OR例如

SELECT SUM(Money) as TotalMoney 
FROM accounts 
HAVING Program='PSC' and
BranchId='10' and 
ExamYear='2013'
于 2013-07-07T12:11:06.973 回答