1单表查询
1.全表查询
语法:select*from 表名称;
描述:查询指定表中的所有数据
2.单条件查询
语法:select*from 表名称 where 条件;
描述:当进行全表查询时,测试指定的条件是否为True,将条件为True的所有数据进行查询展示,其他数据,不予展示
3.多条件查询 并且|或者
语法:
并且:select*from 表名称 where 条件1 and 条件2 and 条件n;
或者:select*from 表名称 where 条件1 or 条件2 or 条件n;
描述:查询指定表中满足要求的数据,并且查询时查询满足所有条件的数据,或者查询时查询满足多个条件中的1个或多个的数据。
4.比较查询
语法:select*from 表名称 where id > 3;
描述:查询id大于3的所有数据。
5.模糊查询
查询条件中的数据,只是查询数据的一部分,通过一部分数据完成整体数据的匹配,成为模糊匹配过程
%:匹配任意字符0~n个
_:匹配任意一个字符
select * from 表名称 where name like ‘员%’; 查询数据表中所有name属性以员开头的数据
select * from 表名称 where name like ‘%员%’; 查询数据表中所有name属性包含员的数据
select * from 表名称 where name like ‘%员’; 查询数据表中所有name属性以员结尾的数据
select * from 表名称 where name like ‘%员\%’; 查询数据表所有name属性以员%结尾的数据
select * from 表名称 where name like ‘员_’;
select * from 表名称 where name like ‘_员_’;
select * from 表名称 where name like ‘_员’;
select * from 表名称 where name like ‘_员\_’;
6.空值查询
语法:
是否为空:select * from 表名称 where 条件 is null;
是否不为空:select * from 表名称 where 条件 is not null;
7.范围查询:in
可以手工指定多个数据,通过范围查询操作符号in,来指定条件取值范围
语法:select*from 表名称 where 字段 in (值1,值2,.....值n);
简化版语法:select*from 表名称where 字段=值1 or字段=值2;
8.范围查询:区间查询
手工指定一个区间范围,包含起始数据和结束数据:between..and..
语法:select*from 表名称 where id between 1 and 6;
9.排序查询
按照指定的列徐进行顺序或者降序排序的数据整理展示过程。
语法:
ORDER BY
select*from 表名称 order by 列[asc(升序)|desc(降序)]
排序查询时,可以一次指定多列进行升序或者降序的指定
select*from 表名称 order by 列1 asc,列2 desc...
10.分页查询
项目开发过程,经常出现分页的操作
【首页,第一页,第二页,,,】
语法:select*from 表名称 limit m,n;查询表中的数据,其实id为m,查询n条
m = n*(page-1)
11.聚合函数
avg() 求取指定列的平均值
count() 求取指定列的数据总数量
select count(*)from dept ; 不推荐使用
select count(1) from dept;
max()求取指定列中数据的最大值
min()求取指定列中数据的最小值
sum()求取指定列中数据的和
12.指定列查询
查询操作中,将数据表中的每一列数据全部展示
在查询过程中,可以通过指定列名称,查询指定的列
select name,age from dept;查询dept中name和age列
13.查询数据剔重
按照指定列,进行重复数据的剔除,主要用于查看或者分析数据差异
select distinct 列 form 表名称;查询数据表中指定列~所有不重复的数据,每个不重复的数据只会出现一次
14.分组查询
出现了很多各种查询,特别有用的分组查询,通过GROUP BY 关键字,将数据表中,指定的列按照组的形式查询分别展示。
SELECT COUNT(1), darea FROM dept GROUP BY darea;
SELECT COUNT(1), darea FROM dept GROUP BY darea having darea is not null;
15.关于查询的疑问
出现的大量的关键词,什么关键词应该写在前面,什么关键词应该写在后面!
SELECT
指定查询列
[FROM 表名称
(1)[WHERE 条件]
(2)[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
(3)[HAVING 分组条件]
(4)[ORDER BY {col_name | expr | position}
[ASC | DESC] , ...]
(5)[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]
2.数据库:多表关联查询
1.直接查询
select*from 表名称1,表名称2
会出来表1行数*表2行数条数据
2.关联查询
关联查询,通过两张表的关联数据,进行条件关联
语法:select*from 表1,表2 where 表1.字段=表2.字段
别名设置:select*from 表1 e,表2 d where e.字段=d.字段
3.子查询
多行子查询
单行子查询
如:select*from emp where dep_id in (select id from dept where dname in('班委','学委'));
单行:select*from emp where dep_id = (select id from dept where dname = '学委')
多行:select*from dept;select*from emp where dep_id in (4,5)
4.连接查询
左外连接(left (outer) join):select*from dept d left join emp e on d.id = e.dep_id;
右外连接(right (outer) join):select*from emp e right join dept d on e.dep_id = d.id;
5.内连接查询
内连接也叫连接,是最早的一种连接。还可以被称为普通连接或者自然连接,内连接是从结果表中删除与其他被连接表中没有匹配行的所有行,所以内连接可能会丢失信息。
语法:select fieldlist from table1 [inner] join table2 on table1.column=table2.column
6.全连接查询:
mysql不支持全外连接,所有只能采取关键字UNION来联合左、右连接的方法
7.hash查询