1.字段的别名
语法:
字段名 as 别名
可以给字段起一个别名,别名可以是中文。as可以省略
例1:通过as 给字段起一个别名
select card as 身份证, name as 姓名,sex as 性别 from students;
例2:别名的as可以省略
select card 身份证, name 姓名,sex 性别 from students;
2.表的别名
语法:
表名 as 别名
as可以省略
例1:通过as 给表students起一个别名
select * from students as stu;
例2:可以省略as
select * from students stu;
3.distinct过滤重复记录
语法:
select distinct 字段名, 字段名 from 表名;
4.where子句
where 后面跟一个条件,实现有选择的查询
语法:
select * from 表名 where 条件;
例 1:查询 students 表中学号 studentNo 等于’001’ 的记录
select * from students where studentNo = '001';
例 2:查询 students 表中年龄 age 等于 30 的姓名 name,班级 class
select name, class from students where age = 30;
5.select查询的基本规律
select * 或者select 字段名 控制了查询返回什么样的字段(列)
where 条件 控制了查询返回什么样的记录(行)
6.比较运算符
=等于
< 小于
<= 小于等于
>大于
>=大于等于
!=和<>不等于
例 1:查询 students 表中 name(姓名)等于’小乔’学生的 age(年龄)
select age from students where name = '小乔';
例 2:查询 students 表中 30 岁以下的学生记录
SELECT * from students where age < 30;
例 3:查询 students 表中 30 岁和30岁以下的学生记录
SELECT * from students where age <= 30;
例 4:查询家乡不在'北京'的学生记录
select * from students where hometown != '北京';
select * from students where hometown <> '北京';
7.逻辑运算符
and与 两个条件必须都满足
条件1 and 条件2
or或 两个条件只要有一个满足即可
条件1 or 条件2
not非 条件成立,not以后就不成立,条件不成立,not以后就成立
not 条件
例 1:查询 age 年龄小于 30,并且 sex 性别为’女’的同学记录
select * from students where age < 30 and sex = '女';
例 2:查询 sex 性别为’女’或者 class 班级为'1 班'的学生记录
select * from students where sex = '女' or class = '1班';
例 3:查询 hometown 老家非’天津’的学生记录
select * from students where not hometown = '天津';
8.模糊查询
like实现模糊查询
%代表任意多个字符
_代表任意一个字符
语法:
字段名 like '字符%'
指定字符开始,后面任意多个字符
例 1:查询 name 姓名中以’孙’开头的学生记录
select * from students where name like '孙%';
例 2:查询 name 姓名以’孙’开头,且名只有一个字的学生记录
select * from students where name like '孙_';
例 3:查询 name 为任意姓,名叫’乔’的学生记录
select * from students where name like '%乔';
例 4:查询 name 姓名有’白’字的学生记录
select * from students where name like '%白%';
9.范围查找
in (值, 值, 值) 非连续范围查找
between 开始值 and 结束值 连续范围查找,包含开始值 包含 结束值
例 1:查询 hometown 家乡是’北京’或’上海’或’广东’的学生记录
select * from students where hometown = '北京' or hometown = '上海' or hometown = '广东';
select * from students where hometown in ('北京', '上海', '广东');
例 2:查询 age 年龄为 25 至 30 的学生记录
select * from students where age >= 25 and age <= 30;
select * from students where age between 25 and 30;
例 3:查询 age 年龄在 20 或 25 或 30 的女生记录
select * from students where age in (20, 25, 30) and sex = '女';
10.空判断
null不是0,也不是'',null在SQL里面代表空,什么也没有
null不能用比较运算符的判断,不能用 字段名 = null 字段名 != null,这些都是错误的
is null ---是否为null
is not null ---是否不为null
例 1:查询 card 身份证为 null 的学生记录
select * from students where card is null;
例 2:查询 card 身份证非 null 的学生记录
select * from students where card is not null;
11.where子句可以用到update和delete语句后面
例 1:修改 age 为 25,并且 name 为’孙尚香’的学生 class 为’2 班’
update students set class = '2 班' where age = 25 and name = '孙尚香';
例 2:删除 class 为’1 班’,并且 age 大于 30 的学生记录
delete from students where class = '1班'and age > 30;
例 3:修改students表中 姓孙的同学,班级为'1班'
update students set class ='1班 'where name like '孙%';
例 4:删除students年龄在20到30之间所有的男同学
delete fromstudents where (age between 20 and 30) and sex = '男';
12.order by排序
语法:
order by 字段名 [asc/desc]
asc代表从小到大,升序,asc可以省略
desc代表从大到小,降序,不可以省略
例 1:查询所有学生记录,按 age 年龄从小到大排序
select * from students order by age;
例 2:查询所有学生记录,按 age 年龄从大到小排序
select * from students order by age desc;
两个字段排序的例子
例 3:查询所有学生记录,按 age 年龄从大到小排序;年龄相同时,再按 studentNo 学号从小到大排序
select * from students order by age desc, studentNo;
当一条select语句出现了where和order by
语法:
select * from 表名 where 条件 order by 字段1, 字段2;
一定要把where写在order by前面
例 4:查询所有男学生记录,按 class 班级从小到大排序;班级相同时,再按 studentNo 学号再按学号从大到小排序
select * from students where sex = '男' order by class, studentNo desc;
13.聚合函数
注意:聚合函数不能用到where后面的条件里
1>count求select返回的记录总数
语法:
count(字段名)
例 1:查询学生总数(查询stuents表有多少记录)
select count(*) from students;
select count(name) from students;
例 2:查询班级总数
select count(distinct class) from students;
例 3:查询女学生总数
select count(*) from students where sex = '女';
select count(name) from students where sex = '女';
select count(sex) from students where sex = '女' ;
2>max查询指定字段里的最大值
语法:
max(字段名)
例 1:查询students中的最大年龄
select max(age) from students;
例 2:查询students中的女生最大年龄
select max(age) from students where sex = '女';
3>min查询指定字段的最小值
语法:
min(字段名)
例 1:查询students中的最小年龄
select min(age) from students;
例 2:查询students中的女生最小年龄
select min(age) from students where sex = '女';
4>sum指定字段的值求和
语法:
sum(字段名)
例 1:查询students中的年龄总和
select sum(age) from students;
例 2:查询students中的女生年龄总和
select sum(age) from students where sex = '女';
5>avg求指定字段的平均值
语法:
avg(字段名)
例 1:查询students中的年龄平均值
select avg(age) from students;
例 2:查询students中的女生年龄平均值
select avg(age) from students where sex = '女';
avg的字段中如果有null,null不做为分母计算平均
create table aa(ageint, namevarchar(10));
insert into aa values(10,'a'), (20,'b'), (null,'c');
select avg(age) from aa; --结果为15,而不是10
14.数据分组
语法:
select 聚合函数 from 表名 where 条件 group by 字段;
select 聚合函数 from 表名 group by 字段;
group by就是配合聚合函数使用的
例 1:分别查询男女同学的数量
select sex,count(*) from students group by sex;
例 2:分别查询'1班'不同性别学生数量
select sex,count(*) from students where class = '1班' group by sex;
例 3: 统计各个班级学生总数、平均年龄、最大年龄、最小年龄。但不统计'3班',统计结果按班级名称从大到小排序
select class 班级, sum(*) 学生总数, avg(age) 平均年龄, max(age) 最大年龄, min(age) 最小年龄 from students where class <> '3班' group by class order by class desc;
where和group by 和order by的顺序
select * from 表名 where 条件 group by 字段 order by 字段
15.分组聚合之后的数据筛选
having子句,总是出现在group by之后
语法:
select * from 表名 group by 字段 having 条件;
用where查询男生总数
where先筛选复合条件的记录,然后再聚合统计
select count(*) from students where sex = '男';
用having查询男生总数
having先分组聚合统计,再在统计的结果中筛选
select count(*) from students group by sex having sex = '男';
having配合聚合函数的使用
where后面条件不能使用聚合函数, having可以使用聚合函数
例:求班级人数大于3人的班级名字
select class from students group by class having count(*) >3;
having与where筛选的区别:
where是对标的原始数据进行筛选;
having是对group by之后已经分过组的数据进行筛选。
having可以使用聚合函数,where不能用聚合函数。
例 1:查询班级总人数大于 2 人的班级名称以及班级对应的总人数;
select class,count(*) from students group by class having count(*) > 2;
例 2:查询平均年龄大于30岁的班级名称和班级总人数
select class,count(*) from students group by class having avg(age) > 30;
16.limit显示指定的记录数
语法:
select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
limit总是出现在select语句的最后
start代表开始行号,行号从0开始编号
count代表要显示多少行
省略start,默认从0开始,从第一行开始
例 1:查询前三行记录
select * from students limit 3;
例 2:查询从第4条记录开始的三条记录
select * from students limit 3, 3;
例 3:查询年龄最大的女同学的name
select name from students where sex = '女' order by age desc limit 1;