distinct字段 —— 消除重复行
例:现在表中的内容为:
使用distinct语句:(distinct 字段 —— 字段相同的消除重复)
select distinct age from student where not is_delete;
比较运算符
< <= > >= != <> =
逻辑运算符
and or not
模糊查询
select 字段 form 表名 where 字段 like 要查询的数据
% 替换任意个
_ 替换1个
例:查询十几岁的学生
select * from student where age like ‘1_’ and not is_delete;
范围查询
表示在一个非连续范围内,使用in:in(1,2,8,9)
例:查询12、22和21岁的学生姓名:
select sname as ‘姓名’ from student where age in(12, 22, 21);
表示连续范围内,使用between … and …:between 20 and 22
例:查询20到22岁的学生姓名:
select sname as ‘姓名’ from student where age between 20 and 22;
空判断:is null;
例:查询年龄为空的信息
select * from student where age is null;
当然,不为空就是:is not null
例:查询年龄不为空的信息(逻辑上未被删除的信息)
select * from student where age is not null and not is_delete;
排序:order by 字段
进行排序时:
asc —— 从小到大进行排序,即升序
desc —— 从大到小进行排序,即降序
默认为:asc,升序
select * from 表名 order by 字段名 [asc/desc];
例:select * from student order by age;
扩展:查询按照年龄在17到30岁降序的学生并且逻辑上没有被删除的学生编号和姓名
select sid as ‘编号’, sname as ‘姓名’ from student where age between 17 and 30 and not is_delete order by age desc;
聚合函数是:对一组值执行计算并返回单一的值。
统计总数:count()
例:统计student表中有多少信息
select count(*) from student;
最大值:max()
例:统计student表中年龄最大的
select max(age) from student;
最小值:min()
例:统计student表中年龄最小的
select min(age) from student;
求和:sum()
例:统计student表中年龄的总和
select sum(age) from student;
平均值:avg()
例:统计student表中年龄的平均值
select avg(age) from student;
分组:group by
select 分组字段 from 表名 group by 分组字段 having 分组的条件;
例:将student表的数据按照age年龄进行分组,讲年龄大于15岁的分为一组。
select group_concat(sname) as ‘姓名’, age as ‘年龄’ from student group by age having age >20;
注意:
group_concat(…) – 查询同种字段中的其他字段group by 进行查询时,select后面只能为所分组的字段,若需要添加表中的其他字段时,需要使用group_concat(字段名)进行连接。
如上面例题中,需要显示姓名应使用group_concat(sname)。若存在多个数据满足一个条件时,将会如上图并列显示group_concat(字段名)的内容。
with rollup:进行汇总
select 分组字段, count(*) from 表名 group by 分组字段 with rollup
select 分组字段, count(*) from 表名 group by 分组字段 with rollup having 条件
with rollup : 将满足的数量进行计数,然后通过count(*)进行输出。
例:将student表按age进行分组并且计数。
select age, count(*) from student group by age with rollup;
将student表按age>1进行分组并且计数。
having(注意having和group by 连用 having后通常也要跟 聚合函数)
limit : 进行分页
limit start, count
注意:limit一般放在最后面
例:student表,每页显示两个,显示第二个页面:
select sid as ‘编号’, sname as ‘姓名’, age as ‘年龄’ from student limit 2, 2;
student表,每页显示5条数据,显示第一个页面,并且数据按年龄从小到大排列:
select sid as ‘编号’, sname as ‘姓名’, age as ‘年龄’ from student order by age asc limit 0, 5;
inner join … on:进行连接查询
select … from 表A inner join 表B on 条件;
例:将student表和grade表中的sname、age、grade查询出来,并且按照成绩的从高到低显示
select student.sname as ‘姓名’, student.age as ‘年龄’, grade.grade as ‘成绩’ from student inner join grade on student.sname = grade.sname order by grade desc;
left join … on :左连接查询(以左边的表为主表,不管是否能在右边找到数据,都显示。)
select … from 表A left join 表B on 条件;
例:将student表中所有学生年龄小于20岁的信息都查询出来,并且将所有学生按照成绩降序排列
select s.sname as ‘姓名’, s.age as ‘年龄’, g.grade as ‘成绩’ from student as s left join grade as g on s.sname = g.sname where s.age < 20 order by g.grade desc;
right join … on :右连接查询(实际上是将右表与左表相调,然后运行左连接查询)
select … from 表A right join 表B on 条件;
将grade表中所有学生成绩大于80的信息都查询出来,并且将所有学生按照年龄升序排列
select g.sname as ‘姓名’, g.grade as ‘成绩’, s.age as ‘年龄’ from student as s right join grade as g on g.sname = s.sname where g.grade > 80 order by s.age asc;
SQL 语句关键字的顺序
写sql语句关键字的顺序:
select – from – join on – where – group by – order by – having – limit
sql语句执行顺序:
from – join on – where – group by – select|having – order by – limit