发布网友 发布时间:2022-05-02 03:49
共2个回答
懂视网 时间:2022-05-02 08:10
create database 数据库名 default charset utf8;
use 数据库名;
1、创建成绩表,字段包括:学生姓名,语文成绩,数学成绩,英语成绩
create table results(name varchar(20),
Chinese float(3,1),
math float(3,1),
English float(3,1)
)charset=utf8;
向表中插入多条数据;
insert into results(name,Chinese,math,English) VALUES
("李三",60,50,80),
("王五",80,40,70),
("李七",76,58,82),
("哈哈",95,82,86),
("李四",83,65,55),
("李六",85,72,87);
查询:
(1) 查询所有学生的数学成绩和总成绩
select name 姓名,math 数学,Chinese+math+English 总成绩 from results;
(2) 查询所有学生的语文和数学成绩和,按从高到低排序
select name 姓名,Chinese+math 总成绩 from results order by Chinese+math desc;
(2) 查询班级总成绩最高的学生姓名
select name 姓名,Chinese+math+English 总成绩 from results order by Chinese+math+English desc limit 1;
select name 姓名,Chinese+math+English 总成绩 from results where Chinese+math+English=(SELECT MAX(Chinese+math+English) from results);
(3) 查询班里所有姓李学生的总成绩最高的姓名
select name 姓名,Chinese+math+English 总成绩 from results where name like ‘李%‘ order by Chinese+math+English desc limit 1;
2、创建一张某超市的购物表,字段包括:商品名,购物价格,商品生茶日期,商品分类;
create table shopping(product_name varchar(20),
price float(6,2),
born_date DATE,
class varchar(20)
)charset=utf8;
向该表中插入多条数据;
INSERT INTO shopping(product_name,price,born_date,class) VALUES
("苹果",20,20170112,"水果"),
("香蕉",80,20170112,"水果"),
("电风扇",120,20170112,"电器"),
("被套",70,20170112,"床上用品"),
("音响",420,20171022,"电器"),
("床单",55,20170305,"床上用品"),
("枕头",20,20170305,"床上用品"),
("草莓",34,20170515,"水果");
查询:(1)每一类商品花的总价格
select class 商品种类,sum(price) 总价格 from shopping group by class;
(2)统每类商品各有多少件
select class 商品种类,count(product_name) 数量 from shopping group by class;
(2)统计水果花了多少钱(两种方式实现)
select class 商品种类,sum(price) 总价格 from shopping where class=‘水果‘;
select class 商品种类,sum(price) 总价格 from shopping group by class having class=‘水果‘;
(3)统计购买的2017-01-12日生产的商品中价格最贵的商品(插入的数据中包括2017-01-12生产的商品)
select product_name 商品名,price 价格 from shopping where born_date=‘2017-01-12‘ order by price desc limit 1;
(4)统一购买商品的总价格
select sum(price) 商品的总价格 from shopping;
mysql第二天作业
标签:ase bsp res database count 统一 价格 class logs
热心网友 时间:2022-05-02 05:18
查询c01号课程最高分