生活随笔
收集整理的這篇文章主要介紹了
MySQL——多表查询练习:电商数据查询
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
多表查詢練習:電商數(shù)據(jù)查詢練習
依據(jù)商品顏色表和商品尺寸表,完成以下練習:
倒序查詢賣的金額最多的產(chǎn)品查詢不同尺碼下的產(chǎn)品銷售數(shù)量查詢不同顏色下的產(chǎn)品銷售金額查詢不同尺碼下的不同顏色的產(chǎn)品銷售金額
create table goodscolor
( ColorID
varchar(4) not null default '-',
ColorNote
varchar(20) not null default '-',
ColorSort
int not null default 0,
pt
varchar(9) not null default '-' );
load data local infile '/Users/zhucan/Desktop/GoodsColor.txt'
into table goodscolor
fields terminated by ','
ignore 1 lines;
create table goodssize
( SizeID
varchar(4) not null default '-',
SizeNote
varchar(100) not null default '-',
SizeSort
int not null default 0,
pt
varchar(9) not null default '-' );
load data local infile '/Users/zhucan/Desktop/GoodsSize.txt '
into table goodssize
fields terminated by ','
ignore 1 lines;
create table OrderDetail
( OrderID
varchar(6) not null default '-',
GoodsID
varchar(6) not null default '-',
GoodsPrice
double not null default 0,
ColorID
varchar(4) not null default '-',
SizeID
varchar(4) not null default '-',
Amount
int not null default 0 );
load data local infile '/Users/zhucan/Desktop/OrderDetail.txt'
into table OrderDetail
fields terminated by '\t'
ignore 1 lines; select * from orderdetail
;
select * from goodscolor
;
select * from goodssize
;
select GoodsID
, sum(GoodsPrice
*amount
) from orderdetail
group by goodsid
order by sum(GoodsPrice
*amount
) desc;
select SizeNote
, sum(amount
) from orderdetail
left join goodssize
on orderdetail
.sizeid
= goodssize
.sizeid
group by orderdetail
.sizeid
order by sum(amount
) desc;
select colornote
as 顏色
, sum(goodsprice
* amount
) as 銷售額
from orderdetail
as od
left join goodscolor
as gc
on od
.colorid
=gc
.colorid
group by od
.colorid
order by sum(goodsprice
* amount
) desc;
select sizenote
,colornote
,sum(goodsprice
* amount
) from orderdetail
as od
left join goodssize
as gs
on od
.sizeid
= gs
.sizeid
left join goodscolor
as gc
on od
.colorid
= gc
.colorid
group by od
.sizeid
, od
.colorid
order by sum(goodsprice
* amount
) desc;
總結(jié)
以上是生活随笔為你收集整理的MySQL——多表查询练习:电商数据查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。