oracle查询返回每行4个重复项

时间:2010-06-14 18:37:03

标签: sql oracle

我正在运行Oracle查询。它似乎工作,除了它返回每个结果的4个重复。这是查询:

Select * from (
    Select a.*, rownum rnum From (
        SELECT NEW_USER.*, NEW_EHS_QUIZ_COMPLETE.datetime
        FROM NEW_USER, NEW_EHS_QUIZ_COMPLETE
        WHERE EXISTS (
            select *
            from NEW_EHS_QUIZ_COMPLETE
            where NEW_USER.id=NEW_EHS_QUIZ_COMPLETE.USER_ID
        )
        ORDER by last_name ASC
    ) a
    where rownum <= #pgtop#
)
where rnum >= #pgbot#

有谁知道为什么这不能正常工作?

1 个答案:

答案 0 :(得分:5)

你在这里有一个交叉联接:

SELECT
    NEW_USER.*,
    NEW_EHS_QUIZ_COMPLETE.datetime
FROM NEW_USER, NEW_EHS_QUIZ_COMPLETE
WHERE EXISTS(
    select * from NEW_EHS_QUIZ_COMPLETE
    where NEW_USER.id=NEW_EHS_QUIZ_COMPLETE.USER_ID
) 

你可能是这个意思:

SELECT
    NEW_USER.*,
    NEW_EHS_QUIZ_COMPLETE.datetime
FROM NEW_USER
INNER JOIN NEW_EHS_QUIZ_COMPLETE
ON NEW_USER.id = NEW_EHS_QUIZ_COMPLETE.USER_ID