ORACLE查询使用NOT IN太多的值

时间:2017-04-07 03:40:28

标签: oracle

我在下面有一个Oracle查询:

SELECT
*
FROM
WA_GA_TBL_ACTIVITY A,
WA_GA_TBL_USERS U
WHERE
A.USERID_FK = U.USERID AND
(A.GROUPID_FK = 'GR0001' OR U.GROUPID_FK = 'GR0001') AND
A.USERID_FK != 'US0007' AND
(A.ACTIVITYID) NOT IN(SELECT ACTIVITYID_FK FROM WA_GA_TBL_ACCESSACTIVITY 
WHERE USERID_FK = 'US0007'

在用户ID = WA_GA_TBL_ACTIVITY时检入US0007的第一个逻辑。 现在我想在NOT IN中获取用户详细信息:

NOT IN(SELECT ACTIVITYID_FK FROM WA_GA_TBL_ACCESSACTIVITY 
WHERE USERID_FK = 'US0007'

当userid_fk = US0007

我试过这样:

SELECT
*
FROM
WA_GA_TBL_ACTIVITY A,
WA_GA_TBL_USERS U
WHERE
A.USERID_FK = U.USERID AND
(A.GROUPID_FK = 'GR0001' OR U.GROUPID_FK = 'GR0001') AND
A.USERID_FK != 'US0007' AND
(A.ACTIVITYID) NOT IN(SELECT S.USERID, S.DATEADDED, W.ACTIVITYID_FK FROM WA_GA_TBL_USERS S, WA_GA_TBL_ACCESSACTIVITY 
W
WHERE S.USERID = W.USERID_FK AND W.USERID_FK = 'US0007')

得到错误:

ORA-00913: too many values

样本数据

表WA_GA_TBL_ACTIVITY

ACTIVITYID | ACTIVITYNAME | USERID_FK | ACTIVITYDATE | GROUPID_FK
001        | add new user | US0001    | 4/6/2017     | GR0001

表WA_GA_TBL_USERS

USERID | USERNAME | DATEADDED
US0001 | testuser1| 4/6/2017
US0007 | testuser2| 4/6/2017

表WA_GA_TBL_ACCESSACTIVITY

ACTIVITYID_FK | USERID_FK

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

IN子查询的投影列数必须与谓词引用的数字匹配。您收到ORA-00913: too many values错误,因为(A.ACTIVITYID)(SELECT S.USERID, S.DATEADDED, W.ACTIVITYID_FK不匹配。

您需要做的就是关联

SELECT
       *
FROM
WA_GA_TBL_ACTIVITY A,
WA_GA_TBL_USERS U
WHERE
A.USERID_FK = U.USERID AND
(A.GROUPID_FK = 'GR0001' OR U.GROUPID_FK = 'GR0001') AND
A.USERID_FK != 'US0007' AND
(A.ACTIVITYID) NOT IN (SELECT W.ACTIVITYID_FK 
                       FROM WA_GA_TBL_USERS S, 
                           WA_GA_TBL_ACCESSACTIVITY W
                           WHERE S.USERID = W.USERID_FK 
                           AND W.USERID_FK = 'US0007')

您似乎可以将该子查询简化为此

 (SELECT W.ACTIVITYID_FK 
   FROM WA_GA_TBL_ACCESSACTIVITY W
   WHERE W.USERID_FK = 'US0007')

但是你比我更了解你的业务逻辑。