我有一个查询,我试图从一个查询中的字段收集计数以避免查询 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;
}
有谁知道这个的正确语法?