SQL异常参数索引超出范围

时间:2014-02-08 06:27:58

标签: mysql sql parameter-passing

我通过jaggery脚本为sql查询发送了4个值。这是sql查询:

SELECT full_name , sum( amount ) AS total 
    FROM hourlyusage , user
    WHERE   DAY = DATE( DATE_SUB( NOW( ) , INTERVAL ? HOUR ) ) 
            AND HOUR BETWEEN HOUR( DATE_SUB( NOW( ) , INTERVAL ? HOUR ) ) 
            AND HOUR( DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) )
            AND hourlyusage.userIp = user.ip_address 
            AND (user.full_name LIKE '%?%' OR user.user_name LIKE '%?%')  GROUP BY full_name

但它给出了这样的例外。 嵌套异常: - java.sql.SQLException:参数索引超出范围(3>参数个数,即2)。

这是什么原因?

1 个答案:

答案 0 :(得分:1)

SELECT full_name , sum( amount ) AS total 
    FROM hourlyusage , user
    WHERE   DAY = DATE( DATE_SUB( NOW( ) , INTERVAL ? HOUR ) ) 
            AND HOUR BETWEEN HOUR( DATE_SUB( NOW( ) , INTERVAL ? HOUR ) ) 
            AND HOUR( DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) )
            AND hourlyusage.userIp = user.ip_address 
            AND (user.full_name LIKE ? OR user.user_name LIKE ?)  GROUP BY full_name

你不能在sql查询中使用通配符,所以尝试从sql中删除通配符并将其添加到值中。正如你在sql中所做的那样'%?%'表示你需要全局匹配。你的值不是在sql查询中。