關於查詢和"1"號的同學學習的課程完全相同的其他同學的信息詳細

以下是建表語句:

create table bdqn_student(
sno number(2),
sname varchar2(20) not null,
sbirthdate date,
sgender char(2));


comment on column bdqn_student.sno
  is '學員編號';
comment on column bdqn_student.sname
  is '學員姓名';
comment on column bdqn_student.sbirthdate
  is '出生日期';
comment on column bdqn_student.sgender
  is '性別';
 alter table bdqn_student  add constraint pk_student primary key(sno);
 alter table bdqn_student  add constraint ck_student_gender check(sgender in('男','女')); 


create table bdqn_teacher(
tno number(2),
tname varchar2(20) not null);
comment on column bdqn_teacher.tno
  is '教師編號';
comment on column bdqn_teacher.tname
  is '教師姓名';
alter table bdqn_teacher  add constraint pk_teacher primary key(tno);
 
create table bdqn_course(
cno number(2),
cname varchar2(20) not null,
tno number(2));


comment on column bdqn_course.cno
  is '課程編號';
comment on column bdqn_course.cname
  is '課程名稱';
comment on column bdqn_course.tno
  is '教師編號';
alter table bdqn_course  add constraint pk_course primary key(cno);  
alter table bdqn_course add constraint fk_course_tno foreign key(tno) references bdqn_teacher(tno);
  
create table bdqn_score(
sno number(2),
cno number(2),
score number(4,1));


comment on column bdqn_score.sno
  is '學員編號';
comment on column bdqn_score.cno
  is '課程編號';
comment on column bdqn_score.score
  is '成績';
  
alter table bdqn_score  add constraint pk_score primary key(sno,cno);  


alter table bdqn_score add constraint fk_score_sno foreign key(sno) references bdqn_student(sno);
alter table bdqn_score add constraint fk_score_cno foreign key(cno) references bdqn_course(cno);
alter table bdqn_score  add constraint ck_score_score check(score between 0 and 100); 




insert into bdqn_student values(1 , '趙雷' , to_date('1990-01-01','yyyy-mm-dd') , '男');
insert into bdqn_student values(2 , '錢電' , to_date('1990-12-21','yyyy-mm-dd') , '男');
insert into bdqn_student values(3 , '孫風' , to_date('1990-05-20','yyyy-mm-dd') , '男');
insert into bdqn_student values(4 , '李雲' , to_date('1990-08-06','yyyy-mm-dd') , '男');
insert into bdqn_student values(5 , '周梅' , to_date('1991-12-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(6 , '吳蘭' , to_date('1992-03-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(7 , '鄭竹' , to_date('1989-07-01','yyyy-mm-dd') , '女');
insert into bdqn_student values(8 , '王菊' , to_date('1990-01-20','yyyy-mm-dd') , '女');
                                                                              




insert into bdqn_teacher values('1' , '陳璇');
insert into bdqn_teacher values('2' , '蔣紅林');
insert into bdqn_teacher values('3' , '韓露');


insert into bdqn_course values(1 , 'web前端' , 2);
insert into bdqn_course values(2 , 'java基礎' , 1);
insert into bdqn_course values(3 , 'web開發' , 3);
insert into bdqn_score values('01' , '01' , 80);
insert into bdqn_score values('01' , '02' , 90);
insert into bdqn_score values('01' , '03' , 99);
insert into bdqn_score values('02' , '01' , 70);
insert into bdqn_score values('02' , '02' , 60);
insert into bdqn_score values('02' , '03' , 80);
insert into bdqn_score values('03' , '01' , 80);
insert into bdqn_score values('03' , '02' , 80);
insert into bdqn_score values('03' , '03' , 80);
insert into bdqn_score values('04' , '01' , 50);
insert into bdqn_score values('04' , '02' , 30);
insert into bdqn_score values('04' , '03' , 20);
insert into bdqn_score values('05' , '01' , 76);
insert into bdqn_score values('05' , '02' , 87);
insert into bdqn_score values('06' , '01' , 31);
insert into bdqn_score values('06' , '03' , 34);
insert into bdqn_score values('07' , '02' , 89);
insert into bdqn_score values('07' , '03' , 98);


commit;



對於  查詢和"1"號的同學學習的課程完全相同的其他同學的信息 這個問題在網上也找了以下看沒有合適的答案自己就寫了,答案如下:

select *

  from bdqn_student

 where sno in (select sno

                 frombdqn_score

                where cno = any (select cnofrom bdqn_scorewhere sno= 1)

                group by sno)



不知道有沒有大神指點下,看下我的解答對不對



這是我之前寫的,但是發現很多地方都不對,當時沒有周全的考慮,早晨研究了下,如果需要課程全部正確的話可以利用PL/SQL 字符串拼接的方法實現,下面是我的實現方法:

declare
  type type_cursor_cno is ref cursor;
  cursor_cno type_cursor_cno;
  v_cno2 varchar2(200);
  v_cno2_all varchar2(200);
  cursor cursor_cno1 is select cno from bdqn_score where sno=1;
  v_cno1 varchar2(200);
  v_cno1_all varchar2(200);
  v_allCount number(10);
begin
      select count(count(sno)) into v_allCount from bdqn_score group by sno;
      open cursor_cno1;
      loop
      fetch cursor_cno1 into v_cno1;
      exit when cursor_cno1%notfound;
      v_cno1:=v_cno1||',';
      v_cno1_all:=v_cno1_all||v_cno1;
      end loop;
      close cursor_cno1;
      for i in 1..v_allCount loop
      open cursor_cno for 'select cno from bdqn_score where sno=:x1' using i;
      loop
          fetch cursor_cno into v_cno2;
        exit when cursor_cno%notfound;
        v_cno2:=v_cno2||',';
        v_cno2_all:=v_cno2_all||v_cno2;
        if v_cno2_all=v_cno1_all then 
          dbms_output.put_line(i);
        end if;    
      end loop;
        v_cno2_all:=null;
      close cursor_cno;
      end loop;
end;

開心!竟然解決了!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章