-- 创建一个班级表(classes),包含字段id,name
CREATE TABLE classes(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10)
)
-- 创建一个学生表(students),包含字段id,name,birthday,gender,score,class_id
CREATE TABLE students(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10) NOT NULL,
birthday DATE,
gender VARCHAR(20),
score INT,
class_id INT
)
-- 在班级表中添加三条记录
INSERT INTO classes (NAME) VALUES ('一年级'),('二年级'),('三年级')
-- 在员工表里添加六条记录
INSERT INTO students (NAME,birthday,gender,score,class_id)
VALUES ('张三','2010-11-10','男','100','1'),
('李四','2012-11-10','女','80','1'),
('王五','2014-11-10','男','60','2'),
('小明','2016-11-10','女','40','2'),
('小李','2018-11-10','女','20','3'),
('小刘','2020-11-10','男','0','3')
-- 查询每个班级的平均分
SELECT c.name,AVG(s.score) FROM students s,classes c GROUP BY class_id
-- 修改张三的名字为张五
UPDATE students SET NAME="张五" WHERE id = 1
-- 删除班级名字为”三年级”的所有学生
DELETE FROM students WHERE class_id = (SELECT id cid FROM classes WHERE NAME='三年级')
-- 查询一年级分数最高的人
SELECT NAME,MAX(score) AS '最高成绩' FROM students WHERE class_id='1';
-- 查询有学生的年级信息(用子查询实现)
SELECT * FROM students WHERE class_id=(SELECT id FROM classes WHERE )
SELECT s.NAME,c.name FROM students s, classes c WHERE s.class_id=c.id
-- 查询以‘小’开头的名字为一个字学生
SELECT * FROM students WHERE NAME LIKE '小%'
-- 查询出生日期在2008-11-10到2012-11-10的学生
SELECT * FROM students WHERE birthday > '2008-11-10' AND birthday < '2012-11-10'
-- 创建一个分类表(cate),包含字段id,name
CREATE TABLE cate(
id INT PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR(20)
)
-- 创建一个商品表(goods),包含字段id,name,number,create_time,price,cate_id
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
gname VARCHAR(10),
number INT,
create_time DATE,
price DOUBLE,
cate_id INT
)
-- 在分类表中添加三条记录 id:1 name:服装,id:2 name:玩具,id:3 name:家电
INSERT INTO cate(cname)
VALUES("服装"),
("玩具"),
("家电")
-- 在商品表里添加六条记录
INSERT INTO goods(gname,create_time,number,price,cate_id)
VALUES('羽绒服','2010-11-10','10','1.00','1'),
('轻便服','2012-11-10','30','2.00','1'),
('汽车','2014-11-10','40','2.00','2'),
('公主','2013-11-10','50','6.00','2'),
('橡皮泥','2015-11-10','23','3.50','2'),
('电饭煲','2008-11-10','3','3.00','3')
-- 使用pymysql模块来查询所有的分类及商品
SELECT * FROM goods g LEFT JOIN cate c ON g.cate_id=c.id
-- 把名字为‘羽绒服’修改成‘棉衣’
UPDATE goods SET gname="棉衣" WHERE gname="羽绒服"
-- 删除名字为‘电饭煲’的记录
DELETE FROM goods WHERE gname="电饭煲"
-- 查询‘服装’分类下面库存最多的商品信息
SELECT g.gname,MAX(g.number) AS "库存最多"
FROM goods g,cate c
WHERE c.cname="服装"
-- 查询分类下有商品分类信息
SELECT c.cname,g.gname
FROM cate c,goods g
WHERE c.id=g.cate_id
-- 查询以‘服’结束的商品信息
SELECT * FROM goods WHERE gname LIKE '%服'
-- 查询创建日期在2008-11-10到2012-11-10的之间的商品
SELECT g.gname,g.create_time
FROM goods g
WHERE g.create_time>'2008-11-10' AND g.create_time<'2012-11-10'
-- 创建一个数据库 cart
CREATE DATABASE cart;
-- 使用新创建的数据库,在数据库下建立两张表
-- 用户表(users): id 姓名 身高
-- 购物车表(user_cart):id 商品名称 购买数量 总价 是否删除 用户id
-- 创建一个分类表(cate),包含字段id,name
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(20),
height DOUBLE
)
CREATE TABLE user_cart(
id INT PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR(20),
number INT,
price DOUBLE,
deleteu INT,
uid INT
)
INSERT INTO users(uname,height)
VALUES("张三","1.80"),
("李四","1.75"),
("小明","1.34")
-- 在购物车表中加入以下记录(1代表未删除,0代表删除)
INSERT INTO user_cart(cname,number,price,deleteu,uid)
VALUES("商品一",10,100,1,1),
("商品二",3,89,0,1),
("商品三",2,30,1,1),
("商品四",10,50,1,2),
("商品五",10,50,1,2),
DELETE user_cart,users FROM user_cart LEFT JOIN users ON user_cart.uid=users.id WHERE user_cart.deleteu=0 AND users.uname="张三"
网友评论