1创建数据库
1.创建BOOK:(图书表)
CREATE TABLE BOOK (
BOOK_ID int(10),
SORT VARCHAR(10),
BOOK_NAME VARCHAR(50),
WRITER VARCHAR(10),
OUTPUT VARCHAR(50),
PRICE int(3));
插入数据
insert into book values(445501,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(445502,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(445503,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(332211,'TP5/10','计算机基础','李伟','高等教育出版社',18.00);
insert into book values(112266,'TP3/12','FoxBASE','张三','电子工业出版社',23.60);
insert into book values(665544,'TS7/21','高等数学','刘明','高等教育出版社',20.00);
insert into book values(114455,'TR9/12','线性代数','孙业','北京大学出版社',20.80);
insert into book values(113388,'TR7/90','大学英语','胡玲','清华大学出版社',12.50);
insert into book values(446601,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(446602,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(446603,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(449901,'TP4/14','FoxPro大全','周虹','科学出版社',32.70);
insert into book values(449902,'TP4/14','FoxPro大全','周虹','科学出版社',32.70);
insert into book values(118801,'TP4/15','计算机网络','黄力钧','高等教育出版社',21.80);
insert into book values(118802,'TP4/15','计算机网络','黄力钧','高等教育出版社',21.80);
2.创建READER:(读者表)
CREATE TABLE READER (
READER_ID int(3),
COMPANY VARCHAR(10),
NAME VARCHAR(10),
SEX VARCHAR(2),
GRADE VARCHAR(10),
ADDR VARCHAR(50));
插入数据
insert into reader values(111,'信息系','王维利','女','教授','1号楼424');
insert into reader values(112,'财会系','李 立','男','副教授','2号楼316');
insert into reader values(113,'经济系','张 三','男','讲师','3号楼105');
insert into reader values(114,'信息系','周华发','男','讲师','1号楼316');
insert into reader values(115,'信息系','赵正义','男','工程师','1号楼224');
insert into reader values(116,'信息系','李 明','男','副教授','1号楼318');
insert into reader values(117,'计算机系','李小峰','男','助教','1号楼214');
insert into reader values(118,'计算机系','许鹏飞','男','助工','1号楼216');
insert into reader values(119,'计算机系','刘大龙','男','教授','1号楼318');
insert into reader values(120,'国际贸易','李 雪','男','副教授','4号楼506');
insert into reader values(121,'国际贸易','李 爽','女','讲师','4号楼510');
insert into reader values(122,'国际贸易','王 纯','女','讲师','4号楼512');
insert into reader values(123,'财会系','沈小霞','女','助教','2号楼202');
insert into reader values(124,'财会系','朱 海','男','讲师','2号楼210');
insert into reader values(125,'财会系','马英明','男','副教授','2号楼212');
3.创建BORROW:(借阅表)
CREATE TABLE BORROW (
READER_ID int(3),
BOOK_ID int(10),
BORROW_DATE DATE);
插入数据
insert into borrow values(112,445501,'2006-3-19');
insert into borrow values(125,332211,'2006-2-12');
insert into borrow values(111,445503,'2006-8-21');
insert into borrow values(112,112266,'2006-3-14');
insert into borrow values(114,665544,'2006-10-21');
insert into borrow values(120,114455,'2006-11-02');
insert into borrow values(120,118801,'2006-10-18');
insert into borrow values(119,446603,'2006-11-12');
insert into borrow values(112,449901,'2006-10-23');
insert into borrow values(115,449902,'2006-8-21');
insert into borrow values(118,118801,'2006-9-10');
2问题及答案
1) 找出姓李的读者姓名(NAME)和所在单位(COMPANY)。
select NAME,COMPANY from reader where name like '李%';
2) 列出图书库中所有藏书的书名(BOOK_NAME)及出版单位(OUTPUT)。
select distinct BOOK_NAME,OUTPUT from book;
3) 查找“高等教育出版社”的所有图书名称(BOOK_NAME)及单价(PRICE),结果按单价降序排序。
SELECT DISTINCT
BOOK_NAME,
PRICE
FROM
book
WHERE
OUTPUT = '高等教育出版社'
ORDER BY
PRICE DESC;
4) 查找价格介于10元和20元之间的图书种类(SORT),结果按出版单位(OUTPUT)和单价(PRICE)升序排序。
SELECT DISTINCT
OUTPUT,
PRICE,
SORT
FROM
book
WHERE
PRICE > '10'
AND PRICE < '20'
ORDER BY
OUTPUT
AND PRICE;
5) 查找书名以”计算机”开头的所有图书和作者(WRITER)。
select BOOK_NAME,WRITER from book where BOOK_NAME like '计算机%';
6) 检索同时借阅了总编号(BOOK_ID)为112266和449901两本书的借书证号(READER_ID)。
SELECT
READER_ID
FROM
borrow
WHERE
BOOK_ID = '112266'
AND ( SELECT READER_ID FROM borrow WHERE BOOK_ID = '449901' );
7) 查找所有借了书的读者的姓名(NAME)及所在单位(COMPANY)。
SELECT DISTINCT
r.NAME,
r.COMPANY
FROM
reader r
LEFT JOIN borrow b ON r.READER_ID = b.READER_ID
WHERE
r.READER_ID = b.READER_ID;
8) 找出李某所借所有图书的书名及借书日期(BORROW_DATE)。
SELECT
o.BOOK_NAME,
b.BORROW_DATE,
b.NAME
FROM
(
SELECT
b.BOOK_ID,
b.BORROW_DATE,
r.NAME
FROM
borrow b
LEFT JOIN ( SELECT READER_ID, NAME FROM reader WHERE NAME LIKE '李%' ) r ON r.READER_ID = b.READER_ID
WHERE
r.READER_ID = b.READER_ID
) b
LEFT JOIN book o ON b.BOOK_ID = o.BOOK_ID;
9) 无重复地查询2006年10月以后借书的读者借书证号(READER_ID)、姓名和单位。
SELECT DISTINCT
b.READER_ID,
b.NAME,
o.OUTPUT
FROM
(
SELECT DISTINCT
b.READER_ID,
b.BOOK_ID,
r.NAME
FROM
( SELECT READER_ID, BORROW_DATE, BOOK_ID FROM borrow WHERE BORROW_DATE >= '2006-10-01' ) b
LEFT JOIN reader r ON b.READER_ID = r.READER_ID
) b
LEFT JOIN book o ON b.BOOK_ID = o.BOOK_ID;
10) 找出借阅了一书的借书证号。
SELECT
reader_id
FROM
borrow
WHERE
book_id IN ( SELECT DISTINCT book_id FROM book WHERE book_name = 'FoxPro大全' );
11) 找出与”赵正义”在同一天借书的读者姓名、所在单位及借书日期 。
SELECT name,company,
(SELECT borrow_date FROM borrow WHERE reader_id =
(SELECT reader_id FROM reader WHERE name='赵正义') )
borrow_date
FROM reader WHERE reader_id = (
SELECT reader_id FROM borrow WHERE borrow_date =
(SELECT borrow_date FROM borrow WHERE reader_id =
(SELECT reader_id FROM reader WHERE name='赵正义') )
AND reader_id != (SELECT reader_id FROM reader
WHERE name='赵正义') );
12) 查询2006年7月以后没有借书的读者借书证号、姓名及单位。日期的关系:昨天 < 今天 < 明天
SELECT
reader_id,
NAME,
company
FROM
reader
WHERE
reader_id NOT IN ( SELECT reader_id FROM borrow WHERE borrow_date >= '2006-08-01' );
13) 求”科学出版社”图书的最高单价、最低单价、平均单价。
SELECT
min( r.PRICE ),
max( r.PRICE ),
avg( r.PRICE )
FROM
( SELECT PRICE FROM book WHERE OUTPUT = '科学出版社' ) r;
14) 求”信息系”当前借阅图书的读书的人数。
SELECT
count( DISTINCT reader_id )
FROM
borrow
WHERE
reader_id IN ( SELECT reader_id FROM reader WHERE company = '信息系' );
15) 求出各个出版社图书的最高价格、最低价格和总册数。
SELECT
max( price ),
min( price ),
count( book_id )
FROM
book
GROUP BY
output;
16) 分别统计出各单位当前借阅图书的读者人数并倒序排列。
SELECT
count( reader_id ),
company
FROM
reader
WHERE
reader_id IN ( SELECT DISTINCT reader_id FROM borrow )
GROUP BY
company;
17) 找出当前至少借阅了2本图书(大于等于2本)的读者姓名及其所在单位。
SELECT NAME,
company
FROM
reader
WHERE
reader_id IN ( SELECT DISTINCT reader_id FROM borrow GROUP BY reader_id HAVING count( book_id )>= 2 );
18) 分别找出借书人次数多于1人次的单位及人次数。
SELECT
company,
count( reader_id )
FROM
reader
WHERE
reader_id IN ( SELECT DISTINCT reader_id FROM borrow )
GROUP BY
company
HAVING
count( reader_id )> 1;
19) 统计找出藏书中各个出版单位的名称、每个出版社的书籍的总册数(每种可能有多册)、书的价值总额。
SELECT
output,
count( book_id ),
sum( price )
FROM
book
GROUP BY
output;
20) 查询经济系是否还清所有图书。如果已经还清,显示该系所有读者的姓名、所在单位和职称。
SELECT NAME,
company,
grade
FROM
reader
WHERE
reader_id NOT IN ( SELECT DISTINCT reader_id FROM borrow )
AND company = '经济系';