字段位置上的sql语句

表: student

表:book

表:borrow


要求1:

查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的学生编号、学生名称、图书编号、图书名称、借出日期

sql语句1

select  a.* ,b.*,c.* from (select * from borrow where T_time>'2007-12-15' and T_time<'2008-1-8' ) a , (select  * from student where  major='计算机') b ,book c
where a.stuID = b.stuID and c.BID = a.BID

sql语句2:

select stuID,(select stuName from student where stuID=borrow.stuID),BID,(select title from book where BID=borrow.BID),T_time from borrow
where stuID in (select stuID from student where major='计算机') and T_time>'2007-12-15' and T_time<'2008-1-8'

两个语句都能达到要求

sql语句2中字段位置上的sql语句在整个sql语句中最后执行。注意:select stuName from student where stuID=borrow.stuID where后的条件链接,

在这里不需要写:select stuName from student ,borrow where stuID=borrow.stuID

原因是:整个sql语句中from后的borrow表已经作为一个基础表了


要求2:

查询目前借书但未归还图书的学生名称及未还图书数量

sql语句1:

select stu.stuName ,b.sl  from student stu,(select  * ,count(stuID) sl from borrow where B_time is null group by stuID) b  where stu.stuID=b.stuID


sql语句2:

select (select stuName from student where stuID=borrow.stuID),count(*) from borrow where B_time is null group by stuID

两条语句都能达到效果。注意sql语句2的特点:字段上的sql



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值