简单查询
select 字段名,... from 表名
where 条件
group by 字段
having 条件
order by 字段
limit X
简单查询
从一个表中找数据
#1 查看students中全部数据【显示的结果有全部字段】
select * from students;
#2 查看students表中的学生的姓名和年龄
select name,age from students ;
条件写法
> 大 于
>= 大于等于
< 小 于
<= 小于等于
<> != 不等于
= 等 于
between .. and .. 在范围之间
like 模糊查询
is null 字段为空
is not null 字段不为空
多个条件组合
and
or
not
大于、小于、等于的条件写法举例
#1 显示年龄大于20的学生的信息
select * from students where age >20;
#2 显示年龄大于20的学生的姓名和年龄
select name,age from students where age >20;
#3 显示id小于5的用户的信息
select * from students where stuid < 5;
#4 显示年龄是20的学生的姓名和年龄
select name,age from students where age = 20;
#5 显示年龄不是20的学生的姓名和年龄
select name,age from students where age !=20;
#6 显示姓名是lin daiyu的学员的信息
select * from students where name = “lin daiyu”;
between .. and ..的用法举例
#1 显示年龄大于20小于30的学员的信息
select * from students where age between 20 and 30;
模糊查询
%:表示任意个任意字符【任意个:0 1 多】
_:表示一个任意字符
#1 显示学生姓名是以a为开头的【字符个数不确定,只是以a为开头】
select * from students where name like “a%”;
#2 显示学生姓名是以a为结尾的【字符个数不确定,最后一个字符是a】
select * from students where name like “%a”;
#3 显示学生姓名中有字母a【字符个数不确定,有a就可以,a可以在开头、结尾、中间】
select * from students where name like “%a%”;
#4 显示学生姓名的第二个字符是a的学生
select * from students where name like “_a%”;
#5 显示学生姓名有两个字符的学生
select * from students where name like “__”;
#6 显示学生姓名三个字符,并且第一个字符是a的学生
select * from students where name like “a__” ;
判断字段为空
#1 查看classid为空的学生的信息
select * from students where classid is null;
#2 查看classid不为空的学生的信息
select * from students where name is not null;
组合条件举例
#1 学生性别是男,而且年龄大于30的学生
select * from students where gender = ”m” and age >30;
#2 找出年龄大于20,并且年龄小于30的学生
select * from students where age >20 and age <30;
#3 找出学生姓名以a为开头,并且性别是M,而且年龄小于20
select * from students where name like “a%” and gender = “m” age <20;
#4 找出年龄小于25或者年龄大于90的学生
select * from students where between age < 25 or age >90;
课堂小练习
# age字段大于20
Where age > 20;
# id字段小于等于3
Where stuid <= 3
# age字段不是15
Where age !=15
# age字段是15
Where age =15
# name字段为空
Where name is null;
# name字段不为空
Where name is not null;
# name字段是以q为开头
Where name like “q%”;
# name字段中包含q
Where name like “%q%”;
# name字段以q为结尾
Where name like “%q”;
# age是3 或者5 或7
#方法1:where age = 22 or age=5 or age=7;
#方法2:where age in (3,5,7);
# age大于等于3且小于等于5
#方法1:where age >= 3 and age <= 5;
#方法2:where age between 3 and 5;
排序:
order by 字段 : 根据指定的字段进行排序
asc:升序【默认是】
desc:降序
指定输出几条记录
默认输出所有符合条件的记录
limit 数 字
案例
#1 将所有的用户按照年龄进行排序
Select * from students order by age;
#2 将所有的用户按照年龄进行排序,而且用降序排列
Select * from students order by age desc;
#3 将所有的用户按照id的降序排序
Select * from students order by stuid desc;
#4 将所有的男性学生按照年龄降序排序
# 第一步:首先找到所有的男性
Select * from students where gender = “m”;
# 第二步:将找到男性按照age进行排序
Select * from students where gender = “m” order by age;
# 第三步:按照降序排序
Select * from students where gender = “m” order by age desc;
#5 查询姓名是以r为开头或者年龄大于50的用户,结构按照stuid降序排序
# 第一步:找到符合条件的数据
Select * from students where name like “r%” or age > 50;
# 第二步:按照id排序
Select * from students where name like “r%” or age > 50 order by stuid;
# 第三步:实现降序排序
Select * from students where name like “r%” or age > 50 order by stuid desc;
#6 查看年龄最大的用户的姓名
# 第一步:查询所有的用户
Select * from students;
# 第二步:让用户按照年龄排序
Select * from students order by age;
# 第三步:实现逆序排序【将年龄最大的用户放到第一个】
Select * from students order by age desc;
# 第四步:输出第一条记录【此时年龄最大的用户就是第一条记录】
Select * from students order by age desc limit 1;
# 第五步:修改语句,指定只显示姓名字段
Select name from students order by age desc limit 1;
#7 查询年龄最小的用户的姓名
# 第一步:查询所有的用户
Select name from students order by age limit 1;
# 第二步:让用户按照年龄排序【将年龄最小的用户放到第一个】
# 第三步:输出第一条记录【此时年龄最小的用户就是第一条记录】
# 第五步:修改语句,指定只显示姓名字段
#8查询姓名是以r为开头或者年龄大于50的用户中id最大的用户的姓名
#第一步:查询符合条件的用户
Select name from students where name like “r%” or age > 50 order by stuid desc limit 1;
#第二步:按照id排序 #第三步:改为降序
#第四步:输出第一条记录# 第五步:只显示姓名字段
#9 询姓名是以r为开头或者年龄大于50的用户中id最大的用户的性别
Select gender from students where name like “r%” or age > 50 order by stuid desc limit 1;
#10 是以r为开头或者年龄大于50的用户中id最大的用户的姓名和性别
Select gender,name from students where name like “r%” or age > 50 order by stuid desc limit 1;
思考题
# 1)查询所有学生信息,按年龄从小到大排序
Select * from students where order by age;
# 2)查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序
Select * from students order by age desc,classid;