我有一个查询,我试图从一个查询中的字段收集计数,以避免查询4次我将它们全部添加到一个查询中。
但是我收到了这个错误:
SQLSTATE[42000]: Syntax error or access violation: 1064
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
')) as english,
sum(if(class='2')) as science,
sum(if(class='3')) as french
F' at line 3
我不确定语法是如何用于此的,所以希望有人可能知道......我的查询是这样的:
$stmt = $pdo->prepare("SELECT
count(id) AS total,
sum(if(class=?)) as english,
sum(if(class=?)) as science,
sum(if(class=?)) as french
FROM school");
try{
$stmt->execute(array(1,2,3));
} catch (PDOException $e){
echo $e -> getMessage(); exit;
}
有没有人知道这个的正确语法?
答案 0 :(得分:1)
至少,你错过了每个总和中的右括号。
然后,我不确定是否(..,..,..)语句,我通常写CASE WHEN(条件)THEN expr1 ELSE expr2 END