日期:2014-05-16 浏览次数:20773 次
CREATE TABLE s(SNO VARCHAR(10),SNAME VARCHAR2(20)); CREATE TABLE c(CNO VARCHAR(10),CNAME VARCHAR2(20),CTEACHER VARCHAR2(10)); CREATE TABLE sc(SNO VARCHAR(10),CNO VARCHAR(10),SCGRADE NUMBER(4)); INSERT INTO s VALUES ('s001','jack'); INSERT INTO s VALUES ('s002','rose'); INSERT INTO s VALUES ('s003','mike'); INSERT INTO c VALUES ('1','history','t01'); INSERT INTO c VALUES ('2','English','t02'); INSERT INTO sc VALUES ('s001','1',90); INSERT INTO sc VALUES ('s001','2',80); INSERT INTO sc VALUES ('s002','1',85); INSERT INTO sc VALUES ('s002','2',90); INSERT INTO sc VALUES ('s003','1',80); INSERT INTO sc VALUES ('s003','2',60); SELECT sno FROM SC t WHERE t.cno='1' AND EXISTS (SELECT 1 FROM SC WHERE sno=t.sno AND cno='2' AND scgrade<t.scgrade); 输出: SNO s001 s003
------解决方案--------------------
select sno from (select t.SNO,t.SCGRADE-nvl(d.SCGRADE ) SCGRADE_c from (SELECT sc.sno, SCGRADE FROM sc where CNO=1) t,(SELECT sc.sno, SCGRADE FROM sc where CNO=2) d
where t.sno = d.sno(+)) t where SCGRADE_c>0