sql“之间的声明”不适用于spring

时间:2013-12-28 12:02:50

标签: java mysql

当我通过sql manager控制台执行语句时,我有这个sql工作正常(给出了结果)。

SELECT * FROM (
                SELECT s.*, @rank
                := @rank + 1 rank
                FROM
                quiz.score s, (SELECT @rank := 0) init
                ORDER BY points DESC
                ) s
                WHERE rank BETWEEN (select count(id) from score)-(10) AND (select count(id) from score)
                ORDER BY rank;

如果我尝试通过spring执行脚本,结果将始终为空:

public List<Score> loadAllScoreLast(String pFrom, String pTo) {
        return createJdbcTemplate().query(mLAUSFT, new Object[] { pFrom, pTo },
                mScoreMapper);
    }
在这种情况下,{p> pFrom (select count(id) from score)-(10)pTo(select count(id) from score)

我认为我错误地包含了pFrompTo

这是原来的sql:

SELECT * FROM (
                SELECT s.*, @rank
                := @rank + 1 rank
                FROM
                quiz.score s, (SELECT @rank := 0) init
                ORDER BY points DESC
                ) s
                WHERE rank BETWEEN ? AND ?
                ORDER BY rank;

有人可以帮我解决这个问题吗?

提前谢谢

的Stefan

1 个答案:

答案 0 :(得分:3)

为什么pFrom和pTo字符串?我想这是类型和/或占位符的一些问题,因为其余的看起来有效。

http://sqlfiddle.com/#!2/45e1b/1

您应该检查生成什么纯SQL。

我想它可能看起来像这样(注意引号)。

SELECT * FROM (
            SELECT s.*, @rank
            := @rank + 1 rank
            FROM
            quiz.score s, (SELECT @rank := 0) init
            ORDER BY points DESC
            ) s
            WHERE rank BETWEEN '(select count(id) from score)-(10)' AND '(select count(id) from score)'
            ORDER BY rank;