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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章