1.查询所有的列
2.查询指定列
3.查询时使用别名
4.查询时添加常量列
5.查询时合并列
6.查询时去除重复记录(distinct)
7.条件查询
8.聚合查询(使用聚合函数查询)
9.分页查询(limit)
10.查询排序
11.分组查询(grop)
12.分组查询后筛选(having)
1.查询所有的列
select * from student;
2.查询指定列
select id,name from student;
3.查询时使用别名
select id as '编号',name as '姓名' from student;
4.查询时添加常量列
select id,name,age,'软件1501' as '班级' from student;
5.查询时合并列
查询每个学生的java和和html总成绩
select id ,name ,(java+html)as '总成绩' from student;
6.查询时去除重复记录(distinct)
select distinct(html) from student;
7.条件查询
1)逻辑条件:and(与) or(或)
2)比较条件: > < >= <= == <>(不等于) between and
3)判断条件(null 空字符串):is null / is not null /=' '(等于空字符串) / <>' '
4)模糊条件:like
- 1)逻辑查询
需求:查询姓名为xiaoming html为88 的学生信息
select * from student where html=88 and name="xiaoming";
需求:查询id为2或者java为98的学生信息
select * from student where id=2 or java=98;
- 2)逻辑查询
需求:查询java大于70小于80的人
select * from student where java>70 and java<80;
需求:查询java大于70小于80的人用between and
select * from student where java between 70 and 80;
- 3)判空查询
需求:查询姓名为空的学生
select * from student where name is null;
- 4)模糊查询
需求:查询所有姓小的人
select * from student where name like '小%';
需求:查询姓小但是名字为两个字的学生信息('小_')
select * from student where name like '小__';
8.聚合查询(使用聚合函数查询)
常用的聚合函数:
1.sum(), 求和
2.avg(), 求平均值
3.max(),求最大值
- min(), 求最小值
5.count() 统计数量,不包含null
需求:查询学生的math总成绩
select sum(math) as 'math的总成绩' from students;
需求:查询学生math的平均成绩
select avg(math) as '数学平均成绩' from students;
需求:查询学生math的最高分
select max(math) from students;
需求:查询学生math的最低分
select min(math) from students;
需求:查询学生math的最低分
select count(id) from students;
9.分页查询(limit)
带两个参数:起始和长度
一般分页查询当前页的数据用:select * from students limit(当前页-1)*每页显示数据条数。
select * from students limit 0,2;
10.查询排序
默认为插入记录的顺序查询
order by 字段 asc/desc
asc:顺序,正序。数据:递增,字母:自然顺序(a-z)
desc:倒序,反序。数据:递减,字母:自然反序(z-a)
select * from students order by name asc;
select * from students order by name desc;
需求:按照english 正序,按照math倒序排列
select * from students order by english asc,math desc;
11.分组查询(grop)
需求:查询成绩相同的人数
select english from students group by english;
select english,count(*) from students group by english;
12.分组查询后筛选(having)
需求:查询成绩相同数大于等于2的人数
select english,count(*) from students group by english having count(*)>1 ;
小练习(下附students.sql):
students.sql
create table students(
id int,
name varchar(20),
chinese int,
english int,
math int
);
select * from students;
insert into students(id,name,chinese,english,math) values(1,'小明',89,78,90);
insert into students(id,name,chinese,english,math) values(2,'小红',67,53,95);
insert into students(id,name,chinese,english,math) values(3,'张小非',87,78,77);
insert into students(id,name,chinese,english,math) values(4,'小花花',88,98,92);
insert into students(id,name,chinese,english,math) values(5,'杨飞',82,84,67);
insert into students(id,name,chinese,english,math) values(6,'张三',55,85,45);
insert into students(id,name,chinese,english,math) values(7,'照小飞',75,65,30);
代码演示:
select * from students;
select name,english from students;
select distinct(english) from students;
select name,chinese as '中文',english as '英语',math as '数学'from students;
select (chinese+english+math) '总成绩' from students where (chinese+english+math)>200;
select name,(chinese+english+math) as '总成绩' from students;
select name,(chinese+english+math+10) as '总成绩' from students;
上接文章:Mysql学习笔记(2)-管理表数据