首页 > 代码库 > SQL语句学习
SQL语句学习
---参考http://www.cnblogs.com/wishyouhappy/p/3700683.html
--案例1 学生选课
--创建表
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
);
--插入数据
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(1,‘Chinese‘);
Insert into S_C(SID,CID,SCORE) values(1,1,80);
Insert into S_C (SID,CID,SCORE) values (1,2,90);
Insert into S_C (SID,CID,SCORE) values (2,4,100);
Insert into S_C (SID,CID,SCORE) values (4,4,90);
Insert into S_C (SID,CID,SCORE) values (4,1,100);
Insert into S_C (SID,CID,SCORE) values (4,3,80);
Insert into S_C (SID,CID,SCORE) values (4,2,80);
Insert into S_C (SID,CID,SCORE) values (2,1,90);
Insert into S_C (SID,CID,SCORE) values (2,4,100);
Insert into S_C (SID,CID,SCORE) values (3,1,60);
--查询,将学生的所有成绩查出来
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图书查阅
--创建表 book
create table book(
bookId varchar2(30), --图书总编号
sortid varchar2(30), --分类号
bookname varchar2(100), --书名
author varchar2(30), --作者
publisher varchar2(100),--出版单位
price number(6,2) --价格,总共允许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) --借阅时间
);
--插入数据-reader
insert into reader(cardid, org, name,gender, title, address)
values (‘xxx‘,‘A‘,‘wish‘,‘1‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘uuu‘,‘A‘,‘luna‘,‘1‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘vvv‘,‘B‘,‘harry‘,‘1‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘www‘,‘C‘,‘chander‘,‘2‘,‘professor‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘yyy‘,‘A‘,‘joey‘,‘2‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘zzz‘,‘B‘,‘richard‘,‘2‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘OOO‘,‘A‘,‘micheal‘,‘2‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘ppp‘,‘A‘,‘richal‘,‘2‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘abp‘,‘A‘,‘michal‘,‘2‘,‘student‘,‘bupt‘);
insert into reader(cardid, org, name,gender, title, address)
values (‘ccp‘,‘A‘,‘mike‘,‘2‘,‘student‘,‘bupt‘);
--插入数据-book
insert into book (bookId,sortid,bookname,author,publisher,price)
values (‘aaa‘,‘a1‘,‘gone with the wind‘,‘CA‘,‘renmin‘,‘103‘);
insert into book (bookId,sortid,bookname,author,publisher,price)
values (‘bbb‘,‘a2‘,‘the little prince‘,‘CB‘,‘jixie‘,‘30‘);
insert into book (bookId,sortid,bookname,author,publisher,price)
values (‘ccc‘,‘a3‘,‘the ordinary world‘,‘CC‘,‘renmin‘,‘130‘);
insert into book (bookId,sortid,bookname,author,publisher,price)
values (‘ddd‘,‘a4‘,‘the little women‘,‘CA‘,‘dianzi‘,‘110‘);
--插入数据-borrow
insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘aaa‘,‘2014-4-29‘);
insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘bbb‘,‘2014-4-29‘);
insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘ccc‘,‘2014-4-28‘);
insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘ccc‘,‘2014-4-28‘);
insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘ddd‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘aaa‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘bbb‘,‘2014-4-28‘);
insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘ddd‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘aaa‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘bbb‘,‘2014-4-28‘);
insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘ddd‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘aaa‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘ccc‘,‘2014-4-26‘);
insert into borrow(cardid,bookid,borrowdate) values(‘vvv‘,‘bbb‘,‘2014-4-28‘);
insert into borrow(cardid,bookid,borrowdate) values(‘vvv‘,‘ddd‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘www‘,‘aaa‘,‘2014-4-27‘);
insert into borrow(cardid,bookid,borrowdate) values(‘www‘,‘ccc‘,‘2014-4-26‘);
select * from book;
select * from reader;
select * from borrow;
--查询A单位借阅图书的读者人数和人员详细信息
--人数
--公用表达式GET
/*
with <name of you cte>(<column names>)
as(
<actual query>
)
select * from <name of your cte>
*/
--count(1) 指定返回第一列的值的数目
--EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False
--exists : 强调的是是否返回结果集,不要求知道返回什么
--exists将外查询表的每一行,代入内查询作为检验,如果内查询返回的结果取非空值,则EXISTS子句返回TRUE,这一行行可作为外查询的结果行,否则不能作为结果。
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,name,org from reader where reader.org=‘A‘)
select cardid,name,org from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);
with vt1 as
(select cardid,name,org from reader where reader.org=‘A‘)
select cardid,name,org from vt1 where exists(select cardid from borrow where borrow.cardid=vt1.cardid);
--查询借书证号尾字符为‘p‘的读者
--模糊查询: %表示任意0个或多个字符,_: 表示任意单个字符。
select cardid,name,org from reader where cardid like ‘%p‘;
--查询名字以m开头的读者,‘1’显示为女,‘2’显示为男
/*
SELECT <myColumnSpec> =
CASE
WHEN <A> THEN <somethingA>
WHEN <B> THEN <somethingB>
ELSE <somethingE>
END
*/
select cardid, name, org,
case when gender=‘1‘ then ‘女‘ when gender=‘2‘ then ‘男‘ else ‘其他‘ end gender
from reader where name like ‘m%‘;
select cardid,name,org,
case when gender=‘1‘ then ‘女‘ when gender=‘2‘ then ‘男‘ else ‘其他‘ end gender
from reader where name like ‘m%‘;
--2014年2-4月借过书的读者
--查询满足条件的读者(仅包含cardid)--未去重
select * from borrow;
select cardid from borrow where borrowdate between ‘2014-2-1‘and ‘2014-5-1‘;
--to_date(date,‘格式‘),把字符串转换为数据库中的日期类型转换函数;
--to_char(date,‘格式‘),是把日期或数字转换为字符串
--日期
--年 yyyy yyy yy year
--月 month mm mon month
--日+星期 dd ddd(一年中第几天) dy day
--小时 hh hh24
--分 mi
--秒 ss
--方法1-3(未去重)
--方法1
select cardid,borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘mm‘)>=‘02‘
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘mm‘)<=‘04‘;
--方法2
select cardid, borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘ --查询
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)>=‘2014-02‘
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)<=‘2014-04‘;
--方法3
select cardid, borrowdate from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);
select cardid,borrowdate from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);
--查询+去重
--关键词 DISTINCT 用于返回唯一不同的值。
select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)>=‘2014-02‘
and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)<=‘2014-04‘;
select distinct cardid from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);
--3)查询+去重+读者姓名等信息
select * from borrow;
with vi as
(select distinct cardid from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘))
select cardid ,name from reader where vi.cardid=reader.cardid;
SQL语句学习