关于SQL的周总结

--任务一:查询每个作者出书的情况(出没出书都要显示)求显示“作者姓名、性别、图书编号、图书名称”
use BooksManager
select AuthorName as 作者姓名,Sex as 性别,BookCode as 图书编号,BookName as 图书名称
from Authors a left join Books b
on a.AuthorID = b.AuthorID


--任务二:查询出书作者的信息,要求显示“作者姓名、性别、图书编号、图书名称”
select AuthorName as 作者姓名,Sex as 性别,BookCode as 图书编号,BookName as 图书名称
from Authors a inner join Books b
on a.AuthorID = b.AuthorID


--任务三:统计九年之内的订单金额最小的前3位会员的信息,要求显示"会员名称、订单总金额"
use BooksManager
select Top 3 Customername as 会员名称, sum(Total) as 总金额
from Customers b inner join Orders a
on b.CustomerID = a.CustomerID
where datediff(yy,OrderDate,getDate())<=9
group by CustomerName
order by sum(total) asc




--任务四:查询制定会员的订单信息
select c.orderid as 订单编号,customername as 会员名称,bookname as 图书名称,d.quantity as 订购数量,orderdate as 订购日期
from ((customers b inner join orders c
on b.CustomerID = c.CustomerID)inner join orderdetails d on c.OrderID = d.OrderID)
inner join Books a
on d.BookCode = a.BookCode
where b.CustomerID = '10019' and datediff(dd,orderdate,getdate())<=180
order by c.OrderID asc,a.BookCode asc




--任务五:查找没有订单的会员
select distinct CustomerName as 客户名称
from(Customers c inner join Orders o
on c.CustomerID = o.CustomerID)right join OrderDetails d on o.OrderID = d.OrderID
where d.Quantity = '1'


--任务6:统计每个会员的订书数量,要求显示“会员名称、订书数量”
select  CustomerName as 客户名称,sum(Quantity) as 订单数量
from(Customers c inner join Orders o
on c.CustomerID = o.CustomerID)inner join OrderDetails d on o.OrderID = d.OrderID
group by CustomerName


--任务7:查询哪些图书还没有被会员订购过,要求显示“图书编号、图书名称”
select b.BookName as 图书名称, b.BookCode as 图书编号
from Books b left join OrderDetails c
on b.BookCode = c.BookCode
where OrderID = 1


--任务8:查询哪个作者的的图书被订购的数量在3本以上的,要求显示“作者姓名,订书数量”
select AuthorName as 作者姓名,sum(o.Quantity) as 订购数量
from (Books b inner join Authors a
on b.AuthorID =a.AuthorID) left join OrderDetails o on b.BookCode = o.BookCode
group by AuthorName
having sum(o.Quantity)>3


--任务9:查询最受欢迎的前3个出版社(被订购数量在前3位),显示“出版社名称、被订购数量”
select top 3 sum(o.Quantity) as 订购数量,PublisherName as 出版社名称
from (Books b inner join OrderDetails o
on b.BookCode = o.BookCode) inner join Publisher p on b.PublisherID = p.PublisherID
group by PublisherName


--任务10:查询哪个类别的图书还没有进货。
select distinct CategoryName as 图书类别
from Categories c left join Books b
on c.CategoryID = b.CategoryID
where Quantity is null

猜你喜欢

转载自blog.csdn.net/wzp15081705152/article/details/80559103
今日推荐