检索列中几个选定行的SUM?

时间:2013-07-07 12:06:45

标签: php mysql sql database

我试图在数据库中获得一些选定的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);

由于死亡功能,这会产生如下错误。

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

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

2 个答案:

答案 0 :(得分:3)

AND之后您错过了Program='PSC'

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

答案 1 :(得分:0)

您需要使用条件运算符AND/OR来支持您的条件

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