长春网站建设公司会展设计效果图
news/
2025/9/27 15:57:12/
文章来源:
长春网站建设公司,会展设计效果图,演出票务网站建设,外贸公司网站建设费会计科目作者前言 欢迎小可爱们前来借鉴我的gtiee秦老大大 (qin-laoda) - Gitee.com
——————————————————————————————
目录
查询数据 条件 逻辑运算符 模糊查询 范围查询 in 判断空 UNION 排序 聚合 分组#xff1a;group by —————————…
作者前言 欢迎小可爱们前来借鉴我的gtiee秦老大大 (qin-laoda) - Gitee.com
——————————————————————————————
目录
查询数据 条件 逻辑运算符 模糊查询 范围查询 in 判断空 UNION 排序 聚合 分组group by ———————————————————————————— 作者小废话 最近小可爱可能发现了我的博客前面一部分重复了这个是我特意这样写的好处有三 1.小可爱看到了可以再温习一遍 2.小可爱可以根据这一部分找到自己的不足也可以进一步分析sql语句 3.其实本人也有复习上一篇博客的习惯这样写可以更加巩固上一天的知识 SQL增删改查 新增 、删除、修改数据 --增加数据
insert into 表名 字段名1,字段名2value内容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 id1 or id4 or id10; 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 piddesc, idasc; 意思是优先对字段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写在后面就相当于对结果进行筛选了
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/919645.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!