如何在oracle中实现查询限制或分页

时间:2012-09-03 09:33:20

标签: oracle

  

可能重复:
  How do I limit the number of rows returned by an Oracle query after ordering?

我正在使用如下查询:

SELECT url, title, metadesc FROM (
        SELECT url,title, metadesc, rownum rn FROM (
                SELECT url, title, metadesc FROM web
            )
            WHERE rownum <= 60
    )
    WHERE rn > 50

它适用于单个表,但如果加入我的主查询 即

SELECT name, id, url, title, metadesc
FROM web, emp

然后它给了我错误:

java.sql.SQLException: ORA-00918: column ambiguously defined

1 个答案:

答案 0 :(得分:1)

使用别名:

SELECT e.name, e.id, w.url, w.title, w.metadesc
  FROM web w
  JOIN emp e ON w.xx = e.yy

或全表名称(更详细):

SELECT emp .name, emp .id, web.url, web.title, web.metadesc
  FROM web
  JOIN emp ON web.xx = emp .yy

如果子查询返回两个具有相同名称的列,则无法执行SELECT *

SQL> SELECT * FROM (
  2     SELECT d1.dummy, d2.dummy
  3       FROM dual d1, dual d2
  4  );

ERROR at line 1:
ORA-00918: column ambiguously defined

在这种情况下,在子查询中使用带有别名的不同列名:

SQL> SELECT * FROM (
  2     SELECT d1.dummy dummy_d1, d2.dummy dummy_d2
  3       FROM dual d1, dual d2
  4  );

D D
- -
X X