数据库的查询
- 查询数据表信息( 基本格式)
- SELECT */col1,... [as "别名"](显示字段) FROM table_name [as "别名"](数据来源) [WHERE condition(搜索条件)] (其他条件);
- [ ]内容更可以根据需要选择
- table_name 可以用database_name.table_name 来表示
- 对显示字段的拓展
- distinct 消除重复行
- 实例:select distinct 字段1,... from 表;
- 作用:在对应的字段前面加上distinct可以消除字段信息的重复内容
- 聚合函数
- 作用:为了快读得到统计数据;
- count 总数的统计
- 示例:select count(*/列名) from students;
- 如果填列名count只统计非空的数据行
- max 最大值的统计
- 示例:select max(id) from students where gender=2;
- min 最小值的统计
- 示例:select min(id) from students where isdelete=0;
- sum 对列进行求和:
- 示例:select sum(age)/count(*) from students where gender=1;
- avg 对列平均值计算
- 示例:select avg(id) from students where is_delete=0 and gender=2
- 对搜索条件的拓展
- where 对字段内容进行筛选 (是对从表中获取的原始数据进行筛选)
- 作用:通过对字段内容限制,对符合条件的字段进行筛选;
- where 配合运算符筛选
- 实例:select * from students where id=1;
- 备注:where 后面支持多种运算符 =/>/>=/!=/<>/and/or/not(优先级:and>or) 等等
- like 模糊查询
- 实例:select * from students where name like '王%';
- 备注:% 表示任意多个任意字符; _表示一个任意字符;
- in/between 范围查询 (只限于数字类型)
- 作用:对在限定范围内的字段进行筛选
- in 范围查询
- 实例:select * from students where id in(1,3,8);
- 备注:括号中的内容不能用“-”来表示范围,必须要指定包含的数字;
- between ..and..范围查询
- 实例:select * from students where id between 3 and 8;
- is null 空判断
- 作用:对字段内容为空/非空的进行筛选;
- is null 判断为空
- select * from students where height is null;
- is not null 判断为非空;
- select * from students where height is not null;
- group by 分组
- 作用:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
- group by 单独使用
- 示例:select gender from students group by gender;
- 备注:
- 当group by单独使用时,实际意义不大。
- group by 后边可以接多个字段,表示按照第一个条件分完组后再按照字段二的要求来进行分组;
- group by + group_concat() 配合显示字段来使用
- 作用:表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
- 示例:select gender,group_concat(name) from students group by gender;
- 会显示gender不同的每个分组下,对应的所有的名字信息
- group by +聚合函数 (计算的是每个组中的集合的运算结果)
- 作用:既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对每个组值得集合做一些操作;
- 示例:select gender,avg(age) from students group by gender;
- 注意:和之前的不同在于这里是在没个组内进行对应的计算并输出结果显示
- group by + having (having表示对group 后的结构进行的筛选 , 与where筛选的内容是不一样的)
- 作用:用来分组查询后指定一些条件来输出查询结果
- 示例:select gender,count(*) from students group by gender having count(*)>2;
- 备注:这里只会显示组内原元素个数总和大于2的组
- group by + with rollup
- 作用:在最后新增一行,可以得到每个分组的汇总级别的数据,表头为null
- 示例:select gender,group_concat(age) from students group by gender with rollup;
- 其他条件的扩展
- order by 对数据进行排序
- 作用:为了方便查看数据的排列情况,方便对数据的查看
- 格式:select * from 表名 order by 字段1 asc|desc [,字段2 asc|desc,...]
- 示例:select * from students where gender=1 and is_delete=0 order by id desc;
- 备注:
- 默认为asc从小到大排列,即升序/ desc从大到小排序,即降序
- 先按照字段一进行排列,当某些行列的值相同的时候,按照2的规则进行排序
- limit 获取部分行
- 作用:当数据量过大时,在一页中查看数据是一件非常麻烦的事情
- 语法:select * from 表明 limit (start),(count)
- 备注:从start下一条开始,获取count条数据
- 示例:select * from students where is_delete=0 limit (n-1)*m,m
- 每页显示m条内容,n为显示第几页
- 对数据来源进行拓展(也可用于其他操作)
- 作用:当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
- 外链接/内连接
- inner join内连接查询(显示进行连接的两表匹配的数据)
- 格式:select * from 表1 inner join 表2 on 表1.列名 = 表2.列名
- 会把表二中和表一 对应字段相同的其他字段的数据和表一的数据集成一个大数据
- left join左连接外连接(在内连接的基础上添加外部数据行)
- 格式:select * from 表1 left join 表2 on 表1.列名 = 表2.列名
- 查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
- right join右连接外连接(在内连接的基础上添加外部数据行)
- 格式:select * from 表1 right join 表2 on 表1.列名 = 表2.列名
- 查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充
- 为了查询简单经常给表起别名
- 例子:select s.name, c.name from students as s inner join classes as c on s.cls_id = c.id;
- 连表查询数据处理的步骤
- 1 from students as s (获取主表的数据)
- 2 inner join classes as c (与外链接表做笛卡尔积)
- 3 on s.cls_id = c.id (设置外链接条件,对笛卡尔积结果进行过滤)
- 自关联(自连接)
- 自关联定义:一个表中的一个字段关联着本表的其他行,就是自关联
- 自关联也可以进行自连查询
- 例子:
- select city.* from areas as city
- inner join areas as province on city.pid=province.aid
- where province.atitle='地名';
子查询
- 概念:
- 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句
- 注意select语句只能嵌套select查询语句,其他语句例如update,delete等,不能嵌套select 语句
- 主查询和子查询的关系
- 子查询是嵌入到主查询中
- 子查询是辅助主查询的,要么充当条件,要么充当数据源
- 子查询是可以独立存在的语句,是一条完整的 select 语句
- 子查询分类
- 标量子查询: 子查询返回的结果是一个数据(一行一列)
- 列子查询: 返回的结果是一列(一列多行)
- 行子查询: 返回的结果是一行(一行多列)
- 常见的查询示例
- 标量子查询
- select * from students where age > (select avg(age) from students);
- 列级子查询
- select name from classes where id in (select cls_id from students);
- 行级子查询
- select * from students where (height,age) = (select max(height),max(age) from students);
总结:命令的执行顺序
- 完整的select语句
- select distinct *
- from 表
- where ....
- (group by ... having ...)
- order by ...
- limit start,count
- 执行顺序为:
- from 表名 从哪个表中取出数据
- where .... 取出符合什么条件的记录
- group by ... 根据哪个字段进行分组
- having ... 分组条件设定
- select distinct 选择上一步产生的数据中的指定属性 distinct表示去重
- order by ... 根据指定属性进行排序 ASC DESC
- limit start,count 取出指定位置的数据
- 实际使用中,只是语句中某些部分的组合,而不是全部