MySQl数据库第八课——-SQL命令查询——-主要命脉
作者前言
欢迎小可爱们前来借鉴我的gtiee秦老大大 (qin-laoda) – Gitee.com
——————————————————————————————
目录
查询数据
条件
逻辑运算符
模糊查询
范围查询 in
判断空
UNION
排序
聚合
分组:group by
————————————————————————————
作者小废话
最近小可爱可能发现了我的博客前面一部分重复了,这个是我特意这样写的,好处有三:
1.小可爱看到了可以再温习一遍,
2.小可爱可以根据这一部分找到自己的不足,也可以进一步分析sql语句
3.其实本人也有复习上一篇博客的习惯,这样写可以更加巩固上一天的知识
SQL增删改查
新增 、删除、修改数据
--增加数据 insert into 表名 (`字段名1`,`字段名2`)value('内容1','内容2'); --删除数据 delete from 表名 where 条件; --修改数据 update 表名 set `字段名`=内容 where 条件;
查询数据
表取别名
--方法1 select * from 表名 as 表别名; --方法2 select * from 表名 表别名;
查看某个字段
--方法1 select 表名.`字段名`from 表名; --方法2 select `字段名` from 表名; --方法3(注意一下,如果表取了别名,使用这个方法就必须使用表别名) select 表别名.`字段名` from 表名 as 表别名 ;
字段取别名
--方法1 select `字段名` as 字段别名 from 表名; --方法2 select 表名.`字段名`as 字段别名 from 表名; --方法3 select 表别名.`字段名` as `字段别名` from 表名 as 表别名;
查看前几行
select * from 表名 limit 数字;
去重
-- 方法1 select distinct * from 表名; -- 方法2 select distinct `字段名` from 表名;
条件
注意一下name 代表字段名 , table_name代表表名
比较运算符
-- 等于 select * from table_name where id = 3; -- 大于 select * from table_name where id = 3; -- 大于等于 select * from table_name where id >= 3; -- 小于 select * from table_name where id < 3; -- 小于等于 select * from table_name where id <= 3; -- 不等于 select * from table_name where id != 3; select * from table_name where id 3;
逻辑运算符
-- 与 select * from table_name where id > 3 and gender = '男'; -- 或 select * from table_name where id > 3 or gender = '男';
and的优先级高于or,但是可以加括号来优先
模糊查询
模糊查询一定是配合
like
使用
-- 下划线 _ 匹配任意一个字符 select * from table_name where name like '周_'; -- % 匹配任意多个字符 select * from table_name where name like '%周';
范围查询
in
-- 取id为1、4、10的人员数据 select * from table_name where id in (1,4,10); select * from table_name where id=1 or id=4 or id=10;
between and
-- between 取连续的数据 select * from table_name where id between 6 and 20; select * from table_name where id >= 6 and id <=20; -- 不同的数据库,sql中between的边界可能不同,有些是包头包尾,有些是包头去尾,或者是不包头也不 包尾
判断空
-- NULL select * from table_name where name is null; -- 非空 select * from table_name where name is not null;
优先级
-- 当无法判断条件运行的优先时,可以使用小括号 select * from table_name where id > 10 and name is null or gender = '男'; select * from table_name where id > 10 and (name is null or gender = '男');
这里就不图片显示了,后面会有图片显示
union
上面所讲的查询都是查询一个表 的内容,如果同事查找多个表该怎么办,这就要利用到nuion了

在拼接过程UNION关联的两张表不在乎字段的名称是否相同。但是要求对应字段的格式和类型一致,而且字段的个数也要一致。
简单的理解为:
1.拼接两张表时,拼接的字段数要相同
2.拼接两张表时,拼接的字段所对应的数据类型要相同
int 和date不能相互转换,但是在mysql可以
--拼接显示前10行 select `id`,`name` from 表名 union select `zi`, `title` from 表名 limit 10;
distinct (去重)
其实union也自带去重效果,但在一些数据库中去重中要写distinct
select `id`,`name` from 表名 union distinct select `id`, `title` from 表名 limit 10

all: 返回所有结果集,包含重复数据。
select `id`,`name` from 表名 union all select `id`, `title` from 表名 limit 10;

排序 order by
默认为升序 (从小到大)
--写法1 select * from city where order by `id`; --写法2 select * from city where order by `id` asc;

对id进行排序
降序(从大到小)
select * from city where order by `id` desc;

多个字段进行排序
select * from city order by `pid`desc, `id`asc;
意思是,优先对字段pid进行排序,排完之后,如果有相同pid数据的再进行id排序,如果没有pid相同的数据就不会进行id排序

聚合
统计个数
-- 统计总数 select count(*) from table_name; select count(0) from table_name; -- 统计id大于3的人数 select count(0) from table_name where id >3; --计算某个字段的数据量 select conut(`字段名`) from 表名 ;
细心的小可爱就会发现count()可以填 * 、数字、字段名 ,下面我来一一讲解一下
count(`字段名`):对这个字段进行计数,如果该字段存在有空值,就不会计算进去,简单理解为只计算非空的数据个数
count(*):会对每一条数据里面的字段值一一遍历一次判断,会造成很大的性能浪费,返回数据的条数
count(数字) :不会对每一条数据里面的字段一一遍历判断,只要写到数据表的每一条数据都会被计算进去,所以建议使用conut(数字)而不是conut(*),
注意一下 conut(数字)里的数字是随意的
最大值
-- 最大值 select max(id) from table_name; -- 性别为女的最大ID select max(id) from table_name where gender='女';

最小值
-- 最小值 select min(id) from table_name; -- 性别为男的最小ID select min(id) from table_name where gender='男';

求和 sum()
-- 求和 select sum(age) from table_name; -- 性别为男的年龄总值 select sum(age) from table_name where gender='男';
记住count 和sum是不一样的 count是计算数量的 sum是计算和的

平均数 avg()
-- 平均值 select avg(age) from table_name; -- 性别为男的年龄平均值 select avg(age) from table_name where gender='男'; select sum(age)/count(0) from table_name where gender='男';

求平均值还可以这样写
select sum(`字段名`)/count(`字段名`) from 表名 ;

分组:group by
注意一下,对哪个字段进行分组,就只能查看那个字段,当写入的字段不参与分组会报错
将查询结果按照字段进行分组,字段值相同的为一组。可用于单个字段分组,也可用于多个字段分组
简单理解就是相同的为一组,比如在一个班级上有很多人。按兴趣分组,相同爱好的为一组,
-- 性别分组 select gender from table_name group by gender;
-- 利用分组去重 select id, name from table_name group by id, name; -- 这里的去重是利用group by进行去重,它的效率会比distinct快,但是要求将进行去重的字段全部写入 --分组内

使用分组也会产生去重效果,
计算分组后,各组里的人数
select `字段名`,count(0)from 表名 group by `字段名`;

如果要加where判断条件的话就要写在group by前面
select * from city where `id`> 3 order by `id`;
分组后的字端拼接
-- 分组后的字段拼接 select gender, group_concat(name) from table_name group by gender; select gender, concat(name) from table_name group by gender;

所谓的字段拼接就是在分组后,我们可以理解为收集该组成员的某样特征
select `pid` ,group_concat(`name`),count(0) from(select * from city limit 4)as a group by `pid` order by `pid`;

括号里面的意思就是获取city表的前四条数据 取别名为a,然后对a表的pid字段进行分组 ,并然后进行升序排序和进行name字段的拼接,
-- 分组后的聚合 -- 各个性别的人数 select gender, count(0) from table_name group by gender; -- 各个性别的平均年龄 select gender, avg(age) from table_name group by gender;
with rollup coalesce()
局部求和
-- 使用coalesce代替空值 select coalesce(gender, 'total'), count(0) num from table_name group by gender - - with rollup;
with rollup 用于局部的聚合,能把某个相同字段值的个数进行统计,并以null命名
coalesce(字段名,‘name’)把字段名里的空值改为name

结果筛选 having
-- 分组后的条件筛选 -- 各个性别的平均年龄大于10的数据 select gender, avg(age) from table_name group by gender having avg(age) >10; -- 各个性别的平均年龄大于10的人数 select gender, count(0) from table_name group by gender having avg(age) >10;
where 是对初始值进行筛选
having 是对结果值进行筛选

总结
如果写where 条件就要写在筛选出结果之前,比如写在group by前面,因为where是初始值进行筛选,where写在后面就相当于对结果进行筛选了,
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/63e5381eff.html
