1>按一定条件查询某字段的数据 (where)
select * from students1 where id>3; ( 查询 id > 3 的数据)
#补充:
select * from students1 where id<3;
注意“ select * from students1; (此命令需谨慎使用, 数据量大可能导致爆内存)
2>给字段取别面并查询 ( 只是便于查询,没有做修改)
select name as ‘姓名‘ ,age as ‘年龄‘ from students1;
3> 给表取别名并查询
select lichao.name lichao.age from students1 as lichao;
4>查看某字段里的数据并消除重复的行 ( 重复的行不显示)
select distinct age from students1;
5>查询id字段里 id大于4 而小于7的数据 (条件查询) ( 满足 两个条件)
select * from students where id>4 and id<7;
#补充
select * from students1 where age>30 and age<60;
#补充2
select * from students1 where age>40 && age<60;
6>条件查询(满足其中一个条件即可)( or)
select * from students1 where id>5 or age<33;
#补充
select * from students1 where id>3 || age<33;
7>模糊查询
select * from studens1 where name like ’%杰%‘; (查找name 类似于 杰的 杰前面和后面可以有任意字符)
#补充
select * from students1 where name like ‘杰%‘;
#补充2
select * from students1 where name like ‘___‘; ( 查找名字为三个字符的人名) ( 一个下斜杠代表一个字符)
#补充3
select * from students1 where name like ’___%‘; ( 至少三个字符)
8> 范围查询 ( 非连续型范围内)
select * from students1 where age in(22,19,44,45);
9>查询 age 在22与34 之间的人数
select * from students1 where age betewn 22 and 34;
#也可以这样写
select * from students1 where ( age between 22 and 34);
#补充 ( 取反)
select * from students1 where age not between 22 and 34;
10>为空判断 、
select * from students1 where high is null; ( 查询 high字段 为空的 某条(些)数据)
11>非空判断
select * from students1 where high is not null;
12>排序
#1从小到大
select * from students1 oder by age asc; ( 按 age 的 从小 到大进行排序)
#2 从大到小
select * from students1 order by age desc; ( 按 age 的 从大到小进行排序)
&补充
select * from students1 where ( age between 22 and 46 ) order by age asc; ( 把 年龄在 22 到 46 岁之间的人 按年龄从小到大进行排序)
&补充2
select * from students1 where ( age between 22 and 34 ) and gander=1 order by high asc; ( 把 年龄在22 和 34 之间的男性 按 身高 从矮到高进行排列 ) ( 包括 22 和 34 )View Code
&补充3
查询年纪在18到34岁的女性,身高从高到矮排序,如果年纪相同的情况下按照身高从小到大排序
select * from students1 where (age between 18 and 34) and gender=2 order by age desc, highasc;
13>聚合函数
&1 查询所有的男人 人数
select count(*) from students1 where gender=1;
&2查询年纪最大的男人
select max(age) from students1 where gender=1;
&3查询年纪最小的女人
select min(age) from students1 where gender=2;
&4 查询所有男人的年龄总和
select sum(age) from students where gender=1;
&5查询所有男人的的平均年龄
select sum(age)/count(*) from students1 where gender=1;
#补充 查询所有男人的的平均年龄
select avg(age) from students1 where gender=1;
&6查询所有男人的的平均年龄 (切保留两位小数)
select round(avg(age),2) from students1 where gender=1;
14>分组
&1按照性别分组查找所有性别
select gender from students1 group by gender;
&2 查询每个性别的人数
select gender,count(*) from students1 group by gender;
#补充
select gender,sum(age) from students1 group by gender;
&3 查询gender组中的姓名
select gender,group_concat(name) from students group by gender;
&4 查询每个性别平均年纪超过30岁的性别,以及姓名 having avg(age) > 40
select gender,group_concat(name) from students1 group by gender having avg(age)>40;
#查询每个性别平均年纪(且保留两位小数)
select gender,group_concat(name),round (avg(age),2) from students1 group bt gender;
&5查询每种性别中的人数多于4个的组的信息
select gender,group_concat(name) from students1 group by gender having count(*)>4;
15>分页
#1分页显示,每页显示2条数据
setelct * from students1 limit 2;
#补充
按照身高从高到矮排序,查找出所有女性,并且分页显示,每页显示2条数据
select * from students1 where gender order desc limit 2;
#补充2
按照年纪从小到大排序,查找出所有男性,并且分页显示,每页显示3 条数据 (年纪若相同,则按身高从高到低排序)
select * from students where gender=1 order by age asc,high desc limit 0,3;