SQL 2005表中数据的查询

表中数据的查询

制作人:丁琪 QQ:854804038

一、在market数据库中:

说明:

market数据库三张表(customers,goods,orders)

goods商品表(name商品名,storage库存量,supplier供应商,status商品状态,price商品价格)

orders订单表(orderid订单编号,goodsname商品名,customerid客户编号,quantity订货数量,ordersum订货金额,orderdate订货日期)

1、从goods表中查询所有商品信息:select * from goods

clip_image002

2、从customers表中查询客户的姓名和电话:select lastname as '用户姓名',tel as '用户电话' from customers

clip_image004

3、查询customers表,显示客户编号和客户姓名,使customerid列显示的列名为“客户编号”,将lastname和firstname列组合起来成为结果集中的“姓名”列:select customerid as '客户编号',(lastname+','+firstname)as '姓名' from customers

clip_image006

4、查看哪些货品有订单。(查询订单表中商品名,去掉重复商品名):select distinct goodsname as '商品名' from orders

clip_image008

5、查看订货金额最大的前三笔订单:select top 3 ordersum as '订货金额' from orders order by ordersum desc

clip_image010

6、查看货品“tv”的货存量情况:select name,storage from goods where name='tv'

clip_image012

7、查看库存量大于4的货品:select name as '货品名称',storage as '库存量' from goods where storage>4

clip_image014

8、查询发生在2007/4/17日到2007/4/18日之间的订单:select orderid,goodsname,customerid,quantity,ordersum,orderdate from orders

where orderdate&gt;='2007-4-17'and orderdate<='2007-4-19'

clip_image016

9、查询北京、上海、天津三地的客户:select firstname,lastname,city from customers where city='北京'or city='上海'or city='天津'

clip_image018

10、查询电话号码第一位不是8的客户:select firstname,lastname,tel from customers where tel not like '8%'

clip_image020

11、查询所有customers表中没有登记电话号码的客户:select * from customers where tel is null

clip_image022

12、查询货品为"tv"且订货金额大于5000的订单:select * from orders where goodsname='tv' and ordersum>5000

clip_image024

13、查找goods表中最贵的定价:select max(ordersum)as '最贵的定价'from orders

clip_image026

14、查询客户的个数:select count(customerid) as '客户的个数' from customers

clip_image028

15、查询订单表中每一种货品的订货总数:select goodsname as '商品名',sum(quantity) as '订货数量' from orders group by goodsname

clip_image030

16、查询订货总数大于500的货品:select sum(quantity) as '订货数量',goodsname as '商品名' from orders group by goodsname

having sum(quantity)&gt;500

clip_image032

17、查询在2007/4/18日之后的订单的金额:select ordersum as '订货金额',orderdate as '订货日期' from orders where orderdate&gt;'2007-4-18'

clip_image034

18、查询在2007/4/18日之后的订单的金额,并统计每种货品的订单总金额啊:select orderid,goodsname,customerid,quantity,ordersum,orderdate from orders

where orderdate&gt;'2007-04-18' order by goodsname compute sum(ordersum) by goodsname

clip_image036

你可能感兴趣的:(sql,数据库,供应商,商品名,制作人)