create table t_type(
tid int(3) PRIMARY key,
tname varchar(10)
)
create table t_goods(
gid int(3) PRIMARY key,
gname varchar(10), – 商品名称
gprice double(5,2),-- 商品价格
gsum int(10),-- 商品数量
tid int(3) – 类型ID
)
– 2.增加数据 添加字段tdate 出厂日期 date
alter table t_goods add tdate date
– 3 按照商品价格从地到高排序 并分页展示 取2-4条数据
select * from t_goods g left join t_type p on g.tid=p.tid order by g.gprice limit 1,
– 4. 查询每种类型商品 平均价格
select avg(g.gprice),p.tname from t_goods g left join t_type p on g.tid=p.tid group by p.tid
– 6. 查询,每类商品中 最高商品的价格,最低商品的价格
select max(g.gprice),min(g.gprice),p.tname from t_goods g left join t_type p on g.tid=p.tid group by p.tid
– 7. 查询每类商品的平均单价 大于 50的商品种类
select avg(g.gprice),p.tname // from t_goods g left join t_type p on g.tid=p.tid // group by p.tid having avg(g.gprice)>50
– 8. 查询 商品名称中 包含 ‘瓜’ 的商品总个数
select count(*) ///from t_goods g left join t_type p on g.tid=p.tid where g.gname like ‘%瓜%’
– 9. 查询所有大于平均价格的商品
select */// from t_goods g left join t_type p on g.tid=p.tid// where g.gprice>(select avg(s.gprice) from t_goods s)
– 10.删除所有低于平均价格的商品
delete from t_goods where gprice<(select * from (select avg(gprice) from t_goods) v)
– 11.查询类型以’器’结尾的出厂日期在2016-09-09之后的商品的平均价格,总量
select avg(g.gprice),sum(g.gsum) from t_goods g left join t_type p on g.tid=p.tid where p.tname like ‘%器’ and g.tdate>‘2016-09-09’
– 12.查询低于电器平均价格电器商品信息
select * from t_goods gs left join t_type ps on gs.tid=ps.tid where gs.gprice<
(select avg(g.gprice) from t_goods g left join t_type p on g.tid=p.tid where p.tname=‘电器’) and ps.tname=‘电器’
– 13.查询家用电器数量大于100 出厂日期在2016-9-9之后的商品信息,并安价格降序排列
select * from t_goods gs left join t_type ps on gs.tid=ps.tid where ps.tname=‘电器’ and gs.gsum>100 and gs.tdate>‘2016-09-09’ order by gs.gprice desc
– 14.查询每种类型价格最高的商品信息商品
select max(gs.gprice),ps.tname,gs.* from t_goods gs left join t_type ps on gs.tid=ps.tid group by ps.tid
– 将商品价格在55以上的水果类型数量统一减少10
update t_goods g set g.gsum=g.gsum-10 where g.tid=4 and g.gprice>55
– 展示表联查数据并分页3到5条数据
select * from t_goods gs left join t_type ps on gs.tid=ps.tid limit 2,4