I want to make a prepared statement for a query that takes a number and calculates an interval. Something along these lines:
SELECT col FROM t1 WHERE createtime < (NOW() - INTERVAL ? DAY)
However, this doesn’t work, because the interval should be a string literal:
SELECT col FROM t1 WHERE createtime < (NOW() - INTERVAL '3 DAY')
But, how should I do this then, without having to concat a query together?
3
Answers
As you observe, the syntax
INTERVAL '3' DAY
allows only a string constant, not a parameter. Usemake_interval
:Maybe this:
You may multiply the one day interval with the bind variable parameter