算术运算符: + - * / %
比较运算符:> < >= <= = != <>
特殊比较运算符:in, not in,is null,is not null,like,between and
逻辑运算符:and or not
逻辑运算符优先级 or<and<not
where条件查询
运算符 可以用在where条件
格式: SELECT 查询信息 FROM 表名 WHERE 条件
* 代表所有
练习:
1.查询年龄不等于18岁的所有用户信息
SELECT * FROM students WHERE age!=18;
2.查询班级为python的所有用户信息
SELECT * FROM students WHERE class='python';
3.查询年龄大于16岁的所有用户信息
SELECT * FROM students WHERE age>16;
4.查询年龄在18到24岁之间的所有用户信息 包含18和24岁;
SELECT * FROM students WHERE age>=18 and age<=24;
SELECT * FROM students WHERE age between 18 and 24;
5.查询年龄不在18到24之间的所有用户信息 not between 不包含开始和结束
SELECT * FROM students WHERE age<18 or age>24;
SELECT * FROM students WHERE age not between 18 and 24;
6.查询id值是1,3,4,6的所有用户信息
SELECT * FROM students WHERE id=1 or id=3 or id=4 or id=6;
SELECT * FROM students WHERE id in (1,3,4,6);
7.查询python班和web班级的所有女生信息
SELECT * FROM students WHERE (class='python' or class='web') and gender='0';
模糊查询 like
%: 任意位的任意字符
_: 一位任意字符
练习
1.查询名字以‘小’开头的用户信息
SELECT * FROM students WHERE name like '小%';
2.查询emial中包含q的所有用户信息
SELECT * FROM students WHERE email like '%q%';
3.查询姓名是两个字的用户信息
SELECT * FROM students WHERE name like '__';
4.查询姓名以小开头并且是两个字的用户信息
SELECT * FROM students WHERE name like '小_';
聚合函数 MAX(),MIN(),SUM(),COUNT(),AVG()
练习
1.统计当前表中一共有多少条数据
SELECT count(*) FROM students;
2.查询python班级最大年龄最小年龄以及平均
SELECT max(age),min(age),avg(age) FROM students WHERE class='python';
3.统计男生的数量
SELECT count(*) FROM students WHERE gender='0';
去除重复的数据:distinct
1.查询所有的班级
SELECT distinct class FROM student;
SELECT class FROM students group by class;
2.查询不重复的班级和性别
SELECT distinct class,gender FROM student;
分组group by
注意:count(*) 在分组中使用时,统计的分别是分组后每个组的数据条数
1.查询表中有哪些班级
SELECT class FROM students GROUP BY class;
2.统计当前表中男生和女生的人数各多少人
SELECT gender,count(*) FROM students GROUP BY gender;
3.统计每个班级的人数
SELECT class,count(*) FROM student GROUP BY class;
4.统计每个班,男生和女生个多少人
SELECT class,gender,count(*) FROM students GROUP BY class,gender;
Having 子句查询
只能在分组后面去使用
类似于where 但是having筛选的是分组后的数据
1.统计每个班级的人数,并查询出班级人数大于2的班级
SELECT class,count(*) FROM students GROUP BY class HAVING count(*)>2;
2.查询每个班级男生人数大于2的班级
SELECT class,gender,count(*) FROM students GROUP BY class,gender HAVING gender='1' and count(*)>2;
先查询出所有的男生 在按照班级分组 然后在用having过滤
SELECT class,count(*) FROM students WHERE gender='1' GROUP BY class HAVING COUNT(*)>2;
排序 order by
desc降序排序
asc 升序排序 默认不写就是升序排序
1.查询所有学员信息 并按照年龄进行降序排序
select * from students order by age desc;
2.查询班级为python的男生信息 并按照年龄进行降序排序
select * from students where class='python' and gender='1' order by age desc;
分页获取数据 limit
limit 1 ; 从前面获取前一条数据
limit 1,1; 跳过第一条数据取一条数据
1.获取前三个学员的数据
select * from students limit 3;
2.获取第二个学员的信息
select * from students limit 1,1;
使用limit实现分页效果
要求 没两条数据为一页
取第一页数据
select * from students limit 0,2; (1-1)*每一页数据的条数
取第二页数据
select * from students limit 2,2; (2-1)*2=2
取第三页
select * from students limit 4,2; (3-1)*2=4
取第五页数据 (5-1)*2=8
select * from students limit (5-1)*2,2;
练习:
1.获取Python班级年龄最大的2个学生
select * from students where class='python' order by age desc limit 2;
2.获取每个班级的平均年龄,并按照平均年龄进行降序排序
select class,avg(age) from students group by class order by avg(age) desc;
3.统计每个班级的人数 获取人数最多的两个班级
select class,count(*) from students group by class order by count(*) desc limit 2;
where group by order by limit顺序问题:
where >group by> order by> limit;
多表查询
1.嵌套查询
将一条查询一句 嵌入到另一条查询语句当中去
1.可做为数据源
2.可以作为查询条件
where 关联查询
只能查询两张表都有对应数据的信息
1.查看每个学生的宿舍信息
select * from student s,dormitory d where s.d_id=d.id;
2.查询小红住在哪个宿舍
select s.id,s.name,d.num from student s,dormitory d where s.d_id=d.id and name='小红';
3.所有专业对应的课程
select * from mojar m,mojar_course m_c,course c where m.id=m_c.m_id and m_c.c_id=c.id;
4.查询python都要学习那些课程
select m.id,m.name,c.name from mojar m,mojar_course m_c,course c where m.id=m_c.m_id and m_c.c_id=c.id and m.name='python';
连接查询
inner join 内链接 结果和where关联查询结果一样
格式: select * from 表1 inner join 表2 on 关联条件
1.查看每个学生的宿舍信息
select * from student s inner join dormitory d on s.d_id=d.id;
left join 外左连接
以左表为准,将坐表中所有的数据查询出来,去右表匹配信息,
如果没有对应的信息以null占位
1.查看每个学生的宿舍信息
select * from student s left join dormitory d on s.d_id=d.id;
right join 外右链接
以右表为准,将右表中所有的数据查询出来,去左表匹配信息
1.查看每个学生的宿舍信息
select * from dormitory d right join student s on d.id=s.d_id;
MySQL查询基本格式:SELECT 查询信息 FROM 表名;运算符:算术运算符: + - * / %比较运算符:> < >= <= = != <>特殊比较运算符:in, not in,is null,is not null,like,between and逻辑运算符:and or not 逻辑运算符优先级 or<and<...