表中數據的查詢
製作人:丁琪 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
2、從customers表中查詢客戶的姓名和電話:select lastname as '用戶姓名',tel as '用戶電話' from customers
3、查詢customers表,顯示客戶編號和客戶姓名,使customerid列顯示的列名爲“客戶編號”,將lastname和firstname列組合起來成爲結果集中的“姓名”列:select customerid as '客戶編號',(lastname+','+firstname)as '姓名' from customers
4、查看哪些貨品有訂單。(查詢訂單表中商品名,去掉重複商品名):select distinct goodsname as '商品名' from orders
5、查看訂貨金額最大的前三筆訂單:select top 3 ordersum as '訂貨金額' from orders order by ordersum desc
6、查看貨品“tv”的貨存量情況:select name,storage from goods where name='tv'
7、查看庫存量大於4的貨品:select name as '貨品名稱',storage as '庫存量' from goods where storage>4
8、查詢發生在2007/4/17日到2007/4/18日之間的訂單:select orderid,goodsname,customerid,quantity,ordersum,orderdate from orders
where orderdate>='2007-4-17'and orderdate<='2007-4-19'
9、查詢北京、上海、天津三地的客戶:select firstname,lastname,city from customers where city='北京'or city='上海'or city='天津'
10、查詢電話號碼第一位不是8的客戶:select firstname,lastname,tel from customers where tel not like '8%'
11、查詢所有customers表中沒有登記電話號碼的客戶:select * from customers where tel is null
12、查詢貨品爲"tv"且訂貨金額大於5000的訂單:select * from orders where goodsname='tv' and ordersum>5000
13、查找goods表中最貴的定價:select max(ordersum)as '最貴的定價'from orders
14、查詢客戶的個數:select count(customerid) as '客戶的個數' from customers
15、查詢訂單表中每一種貨品的訂貨總數:select goodsname as '商品名',sum(quantity) as '訂貨數量' from orders group by goodsname
16、查詢訂貨總數大於500的貨品:select sum(quantity) as '訂貨數量',goodsname as '商品名' from orders group by goodsname
having sum(quantity)>500
17、查詢在2007/4/18日之後的訂單的金額:select ordersum as '訂貨金額',orderdate as '訂貨日期' from orders where orderdate>'2007-4-18'
18、查詢在2007/4/18日之後的訂單的金額,並統計每種貨品的訂單總金額啊:select orderid,goodsname,customerid,quantity,ordersum,orderdate from orders
where orderdate>'2007-04-18' order by goodsname compute sum(ordersum) by goodsname