字段位置上的sql语句

    xiaoxiao2025-03-05  7

    表: 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

    转载请注明原文地址: https://ju.6miu.com/read-1296896.html
    最新回复(0)