我尝试用JDBC4运行以下查询,这会导致语法错误:
SELECT m.*
FROM schema.table m
INNER JOIN (
SELECT :id0 as id, :from0 as date_from, :to0 as date_to
UNION ALL SELECT :id1 as id, :from1 as date_from, :to1 as date_to
) tmp
ON tmp.id = m.id AND m.date BETWEEN tmp.date_from AND tmp.date_to
奇怪的是,当我通过MySQL
(在同一数据库服务器上)运行查询时,查询被接受并运行。
错误消息更加令人困惑,因为第1行没有结束)
。
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 ')' at line 1
服务器运行的是MySQL5.1。
JDBC只接受非常特定位置上的参数。 通常代替标量值。 不能对结构项(如列,表或SQL子句)使用参数。
如果您需要这样做,那么您应该考虑动态SQL或适当的SQL注入。