select语句
student.sql
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,53,95);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);
查询表中所有学生的信息。
select id,name,chinese,english,math from student;
查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;
过滤表中重复数据 实际上先查出所有的然后再剔除重复的
select distinct english from student;
在所有学生分数上加10分特长分
select name,english+10,chinese+10,math+10 from student;
统计每个学生的总分
select name,english+chinese+math from student;
使用别名表示学生分数。
select name,english+chinese+math as sum from student;
where 子句 过滤数据
查询姓名为李一的学生成绩
select * from student where name='李一';
查询英语成绩大于90分的同学
select * from student where english>90;
查询总分大于200分的所有同学
select name,english+chinese+math as sum from student where english+chinese+math>200;
查询英语分数在 80-90之间的同学 上下都包含
select * from student where english between 84 and 85;
查询数学分数为89,90,91的同学。
select * from student where math in(89,90,91);
查询所有姓李的学生成绩
select * from student where name like '李%';
select * from student where name like '李_';
查询数学分>80,语文分>80的同学
select * from student where math>80 and chinese>80;
查询英语>80或者总分>200的同学
select *,english+chinese+math from student where english>80 or english+chinese+math>200;
// order by 默认 asc升序
对数学成绩排序后输出。
select * from student order by math;
对总分排序后输出,然后再按从高到低的顺序输出
select *,english+chinese+math from student order by english+chinese+math desc;
对姓李的学生成绩排序输出 先查出所有的,再过滤,再排序,再按照我们需要的列显示结果
select * from student where name like '李%' order by english;
// count 合计函数
统计一个班级共有多少学生?
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math>90;
统计总分大于240的人数有多少?
select count(*) from student where math+chinese+english>240;
// sum 合计函数
统计一个班级数学总成绩
select sum(math) from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(math),sum(chinese),sum(english) from student;
统计一个班级语文、英语、数学的成绩总和
select sum(math+chinese+english) from student;
统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student;
select sum(chinese)/count(chinese) from student; // 语文缺考的不算
// avg 合计函数
求一个班级语文平均分
select avg(chinese) from student;
求一个班级总分平均分
select avg(chinese+math+english) from student;
求班级最高分和最低分
select max(english) from student;
select name from student where english=(select max(english) from student);
// group by
create table orders(
id int,
product varchar(20),
price float
);
insert into orders(id,product,price) values(1,'电视',900);
insert into orders(id,product,price) values(2,'洗衣机',100);
insert into orders(id,product,price) values(3,'洗衣粉',90);
insert into orders(id,product,price) values(4,'桔子',9);
insert into orders(id,product,price) values(5,'洗衣粉',90);
// 按照商品归类
select * from orders group by product;
// 让多个重复商品的价格加一起来显示
select id,product,sum(price) from orders group by product;
// 找出价格大于150的商品 having 用于 group by的后面,需要用合计函数的情况
select id,product,sum(price) from orders group by product having sum(price)>150;