学生(学号,年龄,性别,系名)
教材(编号,书名,出版社编号,价格)
订购(学号,书号,数量)
出版社(编号,名称,地址)
根据上面基本表的信息完成下列查询。
1统计每位学生订数数量
2 统计每位学生应缴书费。
3 统计订购1000册以上的教材信息。
4 统计没有人定的教材信息。
1、use jiaocai
select student.id,student.age,student.sex,student.dept,count(orders.number) AS 订书数量 from student
left join orders on student.id = orders.s_id
group by student.id,student.age,student.sex,student.dept;
2、select student.id, student.age ,student.sex, student.dept, sum(book.price * orders.number) AS 应缴书费
from student
left join orders on student.id = orders.s_id
left join book on orders.b_id = book.id
group by student.id, student.age ,student.sex, student.dept;
3、由于插入的数据不能满足题目要求,需要修改订购表中的值
select book.id, book.title, book.c_id, book.price
from orders
join book on orders.b_id = book.id
group by book.id, book.title, book.c_id, book.price
having sum(orders.number) >= 1000;
4、组合查询:select jiaocai.bianhao, jiaocai.shuming, jiaocai.chubanshebianhao, jiaocai.jiage
from jiaocai
union
select jiaocai.bianhao, jiaocai.shuming, jiaocai.chubanshebianhao, jiaocai.jiage
from jiaocai
where jiaocai.bianhao not in (select dinggou.shuhao from dinggou);
嵌套查询:select book.id, book.title, book.c_id, book.price
from book
where book.id in (select book.id from book)
or book.id not in (select orders.b_id from orders);