如何仅选择具有活动状态的行

时间:2012-07-13 16:02:46

标签: sql oracle oracle11g

我有这个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查询以选择状态为“Active”的行?

祝福

2 个答案:

答案 0 :(得分: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'";

答案 1 :(得分: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()函数放在那里,因为你问过忽略大小写。