我想获得SQL中的行数。我用
select ROWNUM,FULL_NAME
from OSQS_INSTITUTION
order by FULL_NAME
WHERE rownum BETWEEN 10 AND 20
并试试这个
select ROWNUM,FULL_NAME
from OSQS_INSTITUTION
order by FULL_NAME limit 10,20
并且都返回相同的错误
SQL命令未正确结束
修改
StringBuilder sqlStatement = new StringBuilder();
sqlStatement.append("SELECT USER_NO,FULL_NAME,ACTIVE from");
sqlStatement.append("(");
sqlStatement.append(" SELECT USER_NO,FULL_NAME,ACTIVE, ROW_NUMBER() OVER(ORDER BY full_name) rn");
sqlStatement.append("FROM osqs_institution");
sqlStatement.append(")WHERE rn BETWEEN ? AND ? ");
PreparedStatement pstmt = null;
pstmt = con.prepareStatement(sqlStatement.toString());
pstmt.setInt(1, from);
pstmt.setInt(2, to);
rs = pstmt.executeQuery();
带有必要的尝试和捕获。
答案 0 :(得分:3)
在查询的第一个版本中,您在WHERE
之后指定了ORDER BY
子句,这在语法上是错误的。
第二个版本适用于MySQL,但不适用于Oracle。
在任何一种情况下,当您将其与BETWEEN一起使用时,您将无法获得任何行。
而是试试这个:
WITH qry AS
(
SELECT full_name, ROW_NUMBER() OVER(ORDER BY full_name) rn
FROM osqs_institution
)
SELECT rn, full_name
FROM qry
WHERE rn BETWEEN 10 AND 20
如果您不想使用ROW_NUMBER函数,请尝试以下操作:
SELECT rn, full_name
FROM
(
SELECT full_name, ROWNUM AS rn
FROM (SELECT * FROM osqs_institution ORDER BY full_name)
)
WHERE rn BETWEEN 10 AND 20