我们有两张table。第一个表是员工,第二个表是书籍。
CREATE TABLE EMPLOYEE
( "EMPLOYEE_ID" NUMBER(19,0) not null,
"NAME" VARCHAR2(256),
CONSTRAINT "ID_PK" PRIMARY KEY ("EMPLOYEE_ID") ENABLE
);
CREATE TABLE BOOKS
( "ID" VARCHAR2(32) NOT NULL ENABLE,
"BOOKNAME" VARCHAR2(256),
"EMP_ID" NUMBER(19,0) not null,
CONSTRAINT "BOOK_ID_PK" PRIMARY KEY ("ID") ENABLE,
CONSTRAINT "EMP_FK1" FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE
);
insert into employee values (1, 'James');
insert into employee values (2, 'John');
insert into BOOKS values (1, 'JAVA', 1);
insert into BOOKS values (2, 'CProgramming', 1);
insert into BOOKS values (3, 'JSP', 1);
insert into BOOKS values (4, 'Accountancy', 2);
insert into BOOKS values (5, 'History', 2);
insert into BOOKS values (6, 'Geography', 2);
在上面的代码中,可能有很多书籍,我们不想加载书籍行。因此,我们在where子句中使用rownum。有没有一种方法,我们可以使用rownum,并获得正确的计数,从雇员表只,虽然我们做了一个连接?
select distinct b.EMP_ID from EMPLOYEE e , Books b
where e.EMPLOYEE_ID= b.EMP_ID
and rownum <= 2;
select count(distinct b.EMP_ID) from EMPLOYEE e , Books b
where e.EMPLOYEE_ID= b.EMP_ID
and rownum <= 2;
**Expected output:**
EMP_ID
----------
1
2
1条答案
按热度按时间rjee0c151#
books
表必须以某种方式参与-我建议exists
。要查找其ID存在于BOOKS表中的员工,请运行