I have a query that im trying to collect tallys from fields in one query to avoid querying 4 times i added them all into one query.
But i am getting this error:
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
I’m not sure how the syntax is meant to be for this so was hopeing some one might know… my query is this:
$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;
}
Does any one know the correct syntax for this ?
At least, you have missed a right bracket in each sum line.
Then, Im not sure about if(..,..,..) statement, I usually write CASE WHEN (condition) THEN expr1 ELSE expr2 END