数据概览
goods_color
goods_size
order_detail
绘制E-R关系图
建表导数
create table goods_color(
color_id varchar(6) not null default '-',
color_note varchar(20) not null default '-'
);
load data local infile 'D:/goods_color.txt'
into table goods_color
fields terminated by '\t'
ignore 1 lines;
select * from goods_color;
create table goods_size(
size_id varchar(4) not null default '-',
size_note varchar(100) not null default '-'
);
load data local infile 'D:/goods_size.txt'
into table goods_size
fields terminated by '\t'
ignore 1 lines;
select * from goods_size;
create table order_detail(
order_id varchar(6) not null default '-',
goods_id varchar(6) not null default '-',
goods_price double not null default 0,
color_id varchar(4) not null default '-',
size_id varchar(4) not null default '-',
amount int not null default 0
);
load data local infile 'D:/order_detail.txt'
into table order_detail
fields terminated by '\t'
ignore 1 lines;
select * from order_detail;
业务查询
-- 倒序查询卖的金额最多的产品
select goods_id,sum(goods_price*amount) total_price
from order_detail
group by goods_id
order by total_price desc;
-- 查询不同尺码下的产品销售数量
select size_note,goods_id,sum(amount) gross
from order_detail o left join goods_size g on o.size_id = g.size_id
group by size_note,goods_id;
-- 查询不同颜色下的产品销售金额
select color_note,goods_id,sum(goods_price*amount) total_price
from order_detail o left join goods_color g on o.color_id = g.color_id
group by color_note,goods_id;
-- 查询不同尺码下的不同颜色的产品销售金额
select size_note,color_note,goods_id,sum(goods_price*amount) total_price
from order_detail o left join goods_size gs on o.size_id = gs.size_id
left join goods_color gc on o.color_id = gc.color_id
group by size_note,color_note,goods_id;