mysql实验三:图书管理系统之多表查询实践
声明:本博⽂如存在问题,欢迎各位dalao指正
⽬的与任务:
要求熟练掌握SQL查询语⾔的联接查询、嵌套⼦查询、相关⼦查询及数据更新命令。
联接查询:内连接、左连接、右连接、全连接。
嵌套⼦查询:⼀个查询语句的结果作为另外⼀个查询语句的表或者条件。
相关⼦查询:相关⼦查询的执⾏依赖于外部查询。多数情况下是⼦查询的WHERE⼦句中引⽤了外部查询的表。
--1、内联接查询在流通总库的数据库类书的信息。
select B.ISBN , Bo.bname,Bo.author,Bo.press
from Books B inner join BookInfo Bo
on B.ISBN = Bo.ISBN
where Bo.bname like '%数据库%';
--2、左外联接查询分类为4的⽤户的姓名、单位、及借阅情况。
select u.lname,u.unitName,l.bookNo,l.loanNo,l.borrowDate
from Users u left join Loan l on u.loanNo=l.loanNo
where classNo =4
order by unitName ;
--3、使⽤⼦查询查询与借阅证号为“S06102”的⽤户在同⼀单位的所有⽤户的借阅证号和姓名。
select loanNo,lname
from Users
where unitName =( select unitName
from Users
where loanNo ='S06102');
--4、使⽤⼦查询查询所有借书预约成功的⽤户的姓名和E_mail,以便通知他们。excel教程ppt全套百度网盘
select lname,email
from Users
where loanNo in (select loanNo
from Reservation
where rstatus='T')
--5、使⽤⼦查询查询类别为“教师”的⽤户的借书情况。
创新型国家建设成果丰硕select bookNo,loanNo,borrowDate
from Loan
where loanNo in(select loanNo
from Users
where classNo =(select classNo from Class_User where cname ='教师'));
--6、计算相关⼦查询查询借阅数量⼤于3本的⽤户的借阅证号、姓名、单位。
select u.loanNo,u.lname,u.unitName
flash是什么软件from Users u INNER JOIN (select loanNo,count(*)
from Loan
group by loanNo
having count(*)>2) d
where u.loanNo=d.loanNo;
--7、查询所有曾经借过书号为“A04500049”这本书的所有⽤户的借阅证号和姓名(只考虑loanhist⾥的⽤户)。
select loanNo,lname
from Users
where loanNo in(select loanNo
html版本号有哪些from LoanHist
where bookNo ='A04500049');
--8、查询所有借过书的⽤户借阅证号(包括loan和loanhist的⽤户)。
select loanNo
from Loan
from Loan
union
select loanNo
mysql语句多表查询from LoanHist;
-
-9、查询现在正借有书的⽤户但以前没有借过书的⽤户的借阅证号。
select distinct loanNo
from Loan
where loanNo not in(select loanNo
from LoanHist);
--10、查询当前所有借书信息,并将查询结果导出到’d:\’⽂件中,字段之间⽤逗号分隔。
select *
from Loan into outfile '/var/lib/'FIELDS terminated by '\,';
--11、新建⼀个表loan_statics,包括ISBN和loancount(借阅次数)两个字段,通过查询将每类书的ISBN号和历史借阅次数添加到这个表中。 create table loan_statics(
isbn char(13),
loancount int
);
insert into loan_statics(isbn,loancount)
SELECT isbn,COUNT(*)
linux数据库备份和恢复步骤FROM Books b INNER JOIN (select bookNo
from LoanHist) c
WHERE b.bookNo=c.bookNo
GROUP BY isbn;
--12、在USER表中添加⼀个⾦额字段amount,并对每个⽤户的交费总额进⾏修改。
alter table Users add amount decimal;
update Users set amount=100 where loanNo='S02151';
update Users set amount=110 where loanNo='S02152';
update Users set amount=100 where loanNo='S02153';
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论