首页 > 代码库 > 练习一

练习一

1、创建一张某超市的购物表,字段包括:商品名,购物价格,商品生产日期,商品分类;
CREATE TABLE menu (
id int PRIMARY KEY auto_increment,
name char(10),
price FLOAT (5,2),
product_time DATE ,
class CHAR(10)
)CHARACTER SET utf8;

向该表中插入多条数据;
INSERT INTO menu (name,price,product_time,class) VALUES
("
苹果",20,20170612,"水果"),
("
香蕉",80,20170602,"水果"),
("
水壶",120,20161216,"电器"),
("
被罩",70,20170612,"床上用品"),
("
音响",420,20160216,"电器"),
("
床单",55,20170612,"床上用品"),
("
草莓",34,20170612,"水果");

2
、查询:
(1)每一类商品花的总价格
SELECT class,sum(price) 总价格 FROM menu GROUP BY class;

(2)
统计每类商品各有多少件
SELECT class,count(id) 件数 FROM menu GROUP by class;

(3)
统计水果花了多少钱(两种方式实现)
SELECT class,sum(price) from menu GROUP by class HAVING class="水果";
SELECT class,sum(price) FROM menu WHERE class = "
水果";

(4)
统计购买的2017-01-12日生产的商品中价格最贵的商品(插入的数据中包括2017-01-12生产的商品)
SELECT name,price FROM menu where product_time<20170112 ORDER BY price desc limit 1;

(5)
统计购买商品的总价格
SELECT sum(price) 购买商品总价格 FROM menu;

练习一