练习0516.docx
《练习0516.docx》由会员分享,可在线阅读,更多相关《练习0516.docx(17页珍藏版)》请在冰豆网上搜索。
练习0516
【实验名称】查询练习
【实验拓扑】
【实验环境】
【实验原理】
【实验步骤】
首先是将我们的数据库表添加进来。
1、从goods表中查询所有商品信息。
代码:
usemarketgoselect*fromgoods
2、从customers表中查询客户的姓名和电话。
代码:
selectfirstname,lastname,telfromcustomers
(selectfirstname+lastname,telfromcustomers)
3、查询customers表,显示客户编号和客户姓名,使customerid列显示的列名为“客户编号”,将lastname和firstname列组合起来成为结果集中的“姓名”列。
代码:
selectcustomerIDas'客户编号',(lastname+firstname)as'姓名'fromcustomers
(selectcustomeridas'客户编号',firstname+lastnameas'客户姓名'
fromcustomers)
4、查看哪些货品有订单。
(查询订单表中商品名,去掉重复商品名)
代码:
selectdistinctgoodsnameas'商品名'fromorders
(selectdistinctgoodsnamefromorders)
5、查看订货金额最大的前三笔订单。
代码:
selecttop3ordersumfromordersorderbyordersumdesc
(selecttop3*fromordersorderbyordersumdesc)
6、查看货品“tv”的货存量情况。
代码:
selectname,storagefromgoodswherename='tv'
(selectname,storagefromgoodswherename='tv')
7、查看库存量大于4的货品。
代码:
selectname,storagefromgoodswherestorage>4
(select*fromgoodswherestorage>4)
8、查询发生在2007/4/17日到2007/4/18日之间的订单。
代码:
select*fromorderswhereorderdatebetween'2007-4-17'and'2007-4-19'
(select*fromorderswhereorderdatebetween'2007-4-17'and'2007-4-18')
9、查询北京、上海、天津三地的客户。
代码:
select*fromcustomerswherecitylike'_[京,海,津]'
(select*fromcustomerswherecityin('北京','上海','天津'))
10、查询电话号码第一位不是8的客户。
代码:
select*fromcustomerswheretelnotlike'8%'
(select*fromcustoemrswheretelnotlike'8%')
11、查询所有customers表中没有登记电话号码的客户。
代码:
select*fromcustomerswheretelisnull
(select*fromcustomerswheretelisnotnull)
12、查询货品为"tv"且订货金额大于500的订单。
代码:
select*fromorderswheregoodsname='tv'andordersum>500
(select*fromorderswheregoodsname='tv'andordersum>5000)
13、查找goods表中最贵的定价。
代码1:
selecttop1name,storage,supplier,status,priceas'最贵的定价'fromgoodsorderbypricedesc
(selectmax(price)fromgoods)
代码2:
selecttop1*fromgoodsorderbypricedesc
14、查询客户的个数。
代码:
selectcount(customerID)fromcustomers
(selectcount(*)fromcustomers)
15、查询订单表中每一种货品的订货总数。
代码:
select'产品名称'=goodsname,sum(quantity)as'订货总数'fromordersgroupbygoodsname
(selectgoodsname,sum(quantity)fromordersgroupbygoodsname)
16、查询订货总数大于500的货品。
代码:
select'产品名称'=goodsname,sum(quantity)as'订货总数'fromordersgroupbygoodsnamehavingsum(quantity)>500
(selectgoodsname,sum(quantity)fromordersgroupbygoodsnamehavingsum(quantity)>500)
17、查询在2007/4/18日之后的订单的金额。
代码:
selectorderid,goodsname,customerid,quantity,ordersum,orderdatefromorderswhereorderdate>'2007-04-18'computesum(ordersum)
(selectsum(ordersum)fromorderswhereorderdate>'2007-4-18')
18、查询在2007/4/18日之后的订单的金额,并统计每种货品的订单总金额。
代码:
selectorderid,goodsname,customerid,quantity,ordersum,orderdatefromorderswhereorderdate>'2007-04-18'orderbygoodsnamecomputesum(ordersum)bygoodsname
(selectgoodsname,sum(ordersum)fromorderswhereorderdate>'2007-4-18'
groupbygoodsname)
【实验总结】
单词练习:
goods商品表(name商品名,storage库存量,supplier供应商,status商品状态,price商品价格)
orders订单表(orderid订单编号,goodsname商品名,customerid客户编号,quantity订货数量,ordersum订货金额,orderdate订货日期)