--median select median(score) from student; select classno,median(score) score from student group by classno; ?案例1--学生选课 1. 创建表 stu(学生表),course(课程表),选课表(s_c)
--创建表
create table STU ( id NUMBER not null, name VARCHAR2(255) ) ;
create table COURSE ( id NUMBER not null, coursename VARCHAR2(255) ) ;
create table S_C ( sid NUMBER, cid NUMBER, score NUMBER ); 2.插入数据
--插入数据 Insert into STU (ID,NAME) values (1,‘wish‘); Insert into STU (ID,NAME) values (2,‘rain‘); Insert into STU (ID,NAME) values (3,‘july‘); Insert into STU (ID,NAME) values (4,‘joey‘);
Insert into COURSE (ID,COURSENAME) values (1,‘math‘); Insert into COURSE (ID,COURSENAME) values (2,‘english‘); Insert into COURSE (ID,COURSENAME) values (3,‘Japanese‘); Insert into COURSE (ID,COURSENAME) values (4,‘chinese‘);
Insert into S_C (SID,CID,SCORE) values (1,80); Insert into S_C (SID,2,90); Insert into S_C (SID,SCORE) values (2,4,100); Insert into S_C (SID,SCORE) values (4,SCORE) values (3,60); 3.查询学生选课情况
with vt as (select s.id,s.name,c.coursename,sc.score from stu s,course c,s_c sc where s.id=sc.sid and c.id=sc.cid) select * from vt order by id; 结果:
?
案例2--图书馆借阅 1.创建表: 图书(book),读者(reader),借阅(borrow)
--创建表 book create table book( bookId varchar2(30),--图书总编号 sortid varchar2(30),--分类号 bookname varchar2(100),--书名 author varchar2(30),--作者 publisher varchar2(100),--出版单位 price number(6,2) --价格 );
--创建表 reader create table reader ( cardId varchar2(30),--借书证号 org varchar2(100),--单位 name varchar2(100),--姓名 gender varchar2(2),--性别 title varchar2(30),--职称 address varchar2(100) --地址 );
--创建表 borrow create table borrow( cardId varchar2(30),--借书证号 bookId varchar2(30),--图书总编号 borrowDate varchar2(30) --借阅时间 ); 2.插入数据
--插入数据-book insert into book (bookId,sortid,bookname,author,publisher,price) values (‘aaa‘,‘a1‘,‘gone with the wind‘,‘CA‘,‘renmin‘,‘103‘);
insert into book (bookId,price) values (‘bbb‘,‘a2‘,‘the little prince‘,‘CB‘,‘jixie‘,‘30‘);
insert into book (bookId,price) values (‘ccc‘,‘a3‘,‘the ordinary world‘,‘CC‘,‘130‘);
insert into book (bookId,price) values (‘ddd‘,‘a4‘,‘the little women‘,‘dianzi‘,‘110‘);
--插入数据-reader insert into reader(cardid,org,name,gender,title,address) values (‘xxx‘,‘A‘,‘wish‘,‘student‘,‘bupt‘);
insert into reader(cardid,address) values (‘uuu‘,‘luna‘,address) values (‘vvv‘,‘B‘,‘harry‘,address) values (‘www‘,‘C‘,‘chander‘,‘2‘,‘professor‘,address) values (‘yyy‘,‘joey‘,address) values (‘zzz‘,‘richard‘,address) values (‘OOO‘,‘micheal‘,address) values (‘ppp‘,‘richal‘,address) values (‘abp‘,‘michal‘,address) values (‘ccp‘,‘mike‘,‘bupt‘); --插入数据-borrow insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘aaa‘,‘2014-4-29‘); insert into borrow(cardid,‘bbb‘,‘ccc‘,‘2014-4-28‘); insert into borrow(cardid,borrowdate) values(‘yyy‘,‘ddd‘,‘2014-4-27‘); insert into borrow(cardid,borrowdate) values(‘zzz‘,borrowdate) values(‘uuu‘,‘2014-4-26‘); insert into borrow(cardid,borrowdate) values(‘vvv‘,borrowdate) values(‘www‘,‘2014-4-26‘); 表信息如下:
book------> reader-------> borrow
?
?
?
3. 查询A单位借阅图书的读者人数和人员详细信息
人数:
with vt1 as (select cardid from reader where reader.org=‘A‘) select count(1) from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);
详细信息:
with vt1 as (select cardid,org from reader where reader.org=‘A‘) select cardid,org from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);
4.查询借书证号尾字符为‘p‘的读者
select cardid,org from reader where cardid like ‘%p‘;
5.?查询名字以m开头的女性读者,‘1’显示为女,‘2’显示为男
select cardid, case when gender=‘1‘ then ‘女‘ when gender=‘2‘ then ‘男‘ else ‘其他‘ end gender from reader where name like ‘m%‘;
6.?2014年2-4月借过书的读者
1)查询满足条件的读者(仅包含cardid)--未去重
方式一:
select cardid,borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘ and to_char(to_date(borrowdate,‘mm‘)>=‘02‘ and to_char(to_date(borrowdate,‘mm‘)<=‘04‘; 方式二:
select cardid,‘yyyy‘)=‘2014‘ --查询 and to_char(to_date(borrowdate,‘yyyy-mm‘)>=‘2014-02‘ and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘; 方式三:
select cardid,‘yyyy-mm-dd hh24:mi:ss‘);
2) 查询+去重
select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy‘)=‘2014‘ --查询+去重 and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘; select distinct cardid from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘);
3)查询+去重+读者姓名等信息
with vt1 as (select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy‘)=‘2014‘ and to_char(to_date(borrowdate,‘yyyy-mm‘)>=‘2014-02‘and to_char(to_date(borrowdate,‘yyyy-mm‘)<=‘2014-04‘)select cardid,org from reader where exists (select cardid from vt1 where vt1.cardid=reader.cardid);
(编辑:西安站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|