I have the following SQL query:
SELECT choice, COUNT(*) AS c
FROM Vote, Person
WHERE Vote.pid = Person.pid AND (Person.city = '%s' OR %s IS NULL) <-----
GROUP BY choice
ORDER BY c DESC, choice
I've included the Person.city = '%s' OR %s IS NULL
part so that, if the parameter is NULL
, the query will include all cities. This query works fine when the parameter is NULL
, but when I enter a non-NULL
value, say Paris, I get this error:
Error: column "Paris" does not exist at character 104
And as far as I can tell, this error happens because %s
isn't quoted. But when I do quote it, if %s
is NULL, then '%s' IS NULL
doesn't evaluate to true.
How can I check I check if %s
is NULL
when it's quoted?