0

我有这个 oracle 表:

CREATE TABLE USERS(
        USERID INTEGER NOT NULL,
        GROUPID INTEGER,
        SPECIALNUMBER VARCHAR2(60 ),
        USERNAME VARCHAR2(50 ),
        PASSWD VARCHAR2(50 ),
        DATETOCHANGEPASSWD DATE,
        ADDRESS VARCHAR2(60 ),
        STATEREGION VARCHAR2(50 ),
        COUNTRY VARCHAR2(50 ),
        USERSTATUS VARCHAR2(30 ),
        TELEPHONE VARCHAR2(50 ),
        DATEUSERADDED DATE,
        USEREXPIREDATE DATE,
        DATEUSERLOCKED CHAR(20 ),
        CITY VARCHAR2(50 ),
        EMAIL VARCHAR2(50 ),
        DESCRIPTION CLOB
        )

我使用这个 SQL 查询来获取行:

SqlStatementSmall = "SELECT c.*"
            + " FROM (SELECT b.*, rownum rn"
            + " FROM (SELECT a.*"
            + " FROM USERS a"
            + " ORDER BY %s %s) b"
            + " WHERE rownum <= ?) c"
            + " WHERE rn > ?";

如何修改 SQL 查询以仅选择状态为“活动”的行?

最好的祝愿

4

2 回答 2

2
SqlStatementSmall = "SELECT c.*"
            + " FROM (SELECT b.*, rownum rn"
            + " FROM (SELECT a.*"
            + " FROM USERS a"
            + " ORDER BY %s %s) b"
            + " WHERE rownum <= ?) c"
            + " WHERE rn > ? AND USERSTATUS = 'ACTIVE'";
于 2012-07-13T16:09:37.660 回答
1

这实际上取决于您希望在哪里应用过滤器。我猜你在开始限制 rownum 之前想要它(我想你正在做分页)。在这种情况下,您想要:

SqlStatementSmall = "SELECT c.*"
            + " FROM (SELECT b.*, rownum rn"
            + " FROM (SELECT a.*"
            + " FROM USERS a"
            + " WHERE UPPER(USERSTATUS) = 'ACTIVE'"
            + " ORDER BY %s %s) b"
            + " WHERE rownum <= ?) c"
            + " WHERE rn > ?";

我把 UPPER() 函数放在那里,因为你问忽略大小写。

于 2012-07-13T17:45:42.730 回答