目录:
MySQL-Day02笔记
日期时间函数
- NOW() 返回服务器当前时间 YYYY-MM-DD HH:MM:SS
- CURDATE() 返回当前日期 YYYY-MM-DD
CURTIME() 返回当前时间 HH:MM:SS
year(时间) 返回指定时间的年份
- date(时间) 返回指定时间的日期
- time(时间) 返回指定时间的时间
insert into t11 values(2,”Tom”,year(now()),date(now()),time(now()),now()) - 日期时间运算
- 语法格式
select … from 表名 where
字段名 运算符 (时间 interval 时间间隔单位);
interval :间隔类型关键字
时间间隔单位 :
1 day
2 hour | minute | year | month - 示例
- 查询1天以内的记录
select * from t11 where
meeting > (now() - interval 1 day);
现在时间 - 1天时间 = 1天以前的时间点
解释:
where id > 5
6 > 5
3 …
where meeting > 时间点
2018-05-31 00:00:00 > 2018-05-31 08:00:00 - 查询1天以前3天以内的记录
select * from t11 where
meeting < (now()-interval 1 day) and
meeting > (now()-interval 3 day);
- 查询1天以内的记录
- 语法格式
表字段的操作
- 语法:alter table 表名 执行动作;
- 添加字段(add)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 add 字段名 数据类型 after 字段名; - 删除字段(drop)
alter table 表名 drop 字段名; - 修改字段数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
# 修改数据类型会受到表中原有数据的限制
表记录的管理(删除、更改)
- 删除表记录
- delete from 表名 where 条件;
- 注意
delete语句后如果不加where条件,会将表中所有记录全部删除
- 更改表记录
1、update 表名 set 字段1=值1,字段2=值2 where 条件;
2、注意
update语句后如果不加where条件,会将表中所有记录全部更新
运算符操作
- 数值比较&字符比较
- 数值比较运算符:=、!=、>、>=、<、<=
- 字符比较运算符:=、!=
- 示例
- 查找sanguo表中攻击力大于200的英雄的姓名和攻击力
select name,gongji from sanguo where gongji>200;
- 查找sanguo表中攻击力大于200的英雄的姓名和攻击力
- 逻辑比较
- and (两个或多个条件同时满足)
- or (两个或多个条件有一个条件满足就可以)
- 示例
- 找出攻击值大于200的蜀国英雄的名字和攻击值
select name,gongji from sanguo where gongji>200 and country=”蜀国”; - 将吴国英雄中攻击值为110的英雄的攻击值设置为100,防御值设置为60
update sanguo set gongji=100,fangyu=60 where country=”吴国” and gongji=110; - 查找蜀国和魏国的英雄
select * from sanguo where country=”蜀国” or country=”魏国”;
- 找出攻击值大于200的蜀国英雄的名字和攻击值
- 范围内比较
- 运算符
where 字段名 between 值1 and 值2 # 值为数字
where 字段名 in(值1,值2,…)
where 字段名 not in(值1,值2,…) - 示例
- 查找攻击值在100-200之间的蜀国英雄信息
select * from sanguo where gongji between 100 and 200 and country=”蜀国”; - 查找编号为1、3或5的蜀国英雄 和 貂蝉的 编号、姓名和国家
select id,name,country from sanguo where id in(1,3,5) and country=”蜀国” or name=”貂蝉”; - 查找蜀国和吴国以外的国家的女英雄信息
select * from sanguo where country not in(“蜀国”,”吴国”) and sex=”女”;
- 查找攻击值在100-200之间的蜀国英雄信息
- 运算符
- 匹配空、非空
- 空 :is null
- 非空:is not null
- 示例
- 查找姓名为NULL的蜀国女英雄信息
select * from sanguo where name is null and country=”蜀国” and sex=”女”; - 查找姓名为”“的英雄的 id 姓名 和 国家
select id,name,country from sanguo where name=”“;
- 查找姓名为NULL的蜀国女英雄信息
- 注意
- null :空值,必须用is 或者 is not 去匹配
- “” :空字符串,只能用 = 或者 != 去匹配
- 模糊比较
- 语法格式
where 字段名 like 表达式 - 表达式
1、_ :匹配单个字符
2、% :匹配0到多个字符 - 示例
# 匹配名字中至少有2个字符的记录
select name from sanguo where name like “%“;
# 匹配所有记录(NULL除外)
select name from sanguo where name like “%”;
# 匹配名字中有三个字符的记录
select name from sanguo where name like “_“;
# 匹配姓 赵 的英雄记录
select name from sanguo where name like “赵%”;
- 语法格式
SQL查询
- 总结(执行顺序)
3、select …聚合函数 from 表名
1、where …
2、group by …
4、having …
5、order by …
6、limit … - order by(给查询结果进行排序)
- order by 字段名 排序方式
- 排序方式
- ASC(默认) :升序
- DESC :降序
- 示例
- 将英雄按防御值从低到高排序
select * from sanguo order by fangyu ASC; - 将蜀国英雄按攻击值从高到低排序
select * from sanguo where country=”蜀国” order by gongji desc; - 将魏蜀两国男英雄中名字为三个字的英雄按防御值升序排列
select * from sanguo where country in(“魏国”,”蜀国”) and sex=”男” and name like “_” order by fangyu ASC;
- 将英雄按防御值从低到高排序
- limit(永远放在SQL语句的最后写)
- 作用 :限制显示查询记录的个数
- 用法
- limit n –>显示 n 条记录
- limit m,n –>从 m+1 条记录开始,显示 n 条记录
limit 2,3 –>显示3、4、5三条记录
## m的值是从0开始计数,2则表示第3条记录
- 示例
1 . 在蜀国英雄中,查找防御值倒数第二名到倒数第四名的英雄记录
select * from sanguo where country=”蜀国” order by fangyu ASC limit 1,3;
- 在蜀国且名字不为空值的英雄中,查找攻击值前三名的英雄的姓名、攻击值和国家
select name,gongji,country from sanguo where country=”蜀国” and name is not null order by gongji desc limit 3;
- 在蜀国且名字不为空值的英雄中,查找攻击值前三名的英雄的姓名、攻击值和国家
- 聚合函数
- 分类
- avg(字段名) : 求该字段的平均值
- sum(字段名)
- max(字段名)
- min(字段名)
- count(字段名) : 统计该字段记录的个数
- 示例
- 攻击力最强值
select max(gongji) from sanguo; - 统计表中 id 和 name 字段分别有多少条记录
select count(id),count(name) from sanguo;
## 控制NULL不会被统计,空字符串”“会被统计 - 统计蜀国英雄中攻击值 >200 的英雄个数
select count(*) from sanguo where country=”蜀国”;
- 攻击力最强值
- 分类
- group by(分组)
- 作用 :给查询结果进行分组
- 用法 :group by 字段名
- 示例
- 计算一下每个国家的平均攻击力
select country,avg(gongji) from sanguo
group by country;
1、分组 2、聚合 3、去重
蜀国
蜀国 蜀国平均值 蜀国
蜀国
魏国 魏国平均值 魏国
魏国
吴国 吴国平均值 吴国 - 查找所有国家中 英雄数量最多的前2名国家的 国家名称和英雄数量
select country,count(*) as number from sanguo
group by country
order by number desc
limit 2;
- 计算一下每个国家的平均攻击力
- 注意
1、group by后的字段名必须要为select之后的字段名,如果查询字段和group by之后的字段不一致,则必须要对该字段进行聚合处理(聚合函数)
- having
- 作用 :对查询的结果进行进一步的筛选
- 示例
- 找出平均攻击力 >105 的国家的前2名,显示国家名和平均攻击力
select country,avg(gongji) from sanguo
group by country
having avg(gongji) > 105
order by avg(gongji) desc
limit 2;
- 找出平均攻击力 >105 的国家的前2名,显示国家名和平均攻击力
- 注意
- having语句通常与group by语句联合使用,用来过滤由group by语句返回的结果集
- where只能操作表中实际存在的字段(desc 表名;),having操作的是由聚合函数生成的显示列
- distinct(不显示字段的重复值)
- 用法
select distinct 字段名1,字段名2… from 表名; - 示例
1、统计sanguo表中一共有多少个国家
select distinct country from sanguo;
2、计算蜀国一共有多少个英雄
select count(distinct id) as number from sanguo;
3、注意
1、distinct处理的是distinct 和 from 之间的所有字段,所有字段的值必须全部相同才能去重
- 用法
- 查询表记录时做数学运算
- 运算符
-
-
- / %
2/. 示例
1、查询时显示所有英雄攻击力翻倍
select id,name,gongji*2 as new_gj,country from sanguo;
- / %
-
-
- 运算符
约束
- 作用
为了限制无效的数据插入到数据表中 - 约束分类
- 默认约束(default)
- 作用 :插入记录时,不给该字段赋值,使用默认值
- 格式 :字段名 数据类型 default 值
- 非空约束(not null)
- 作用 :不允许该字段的值有NULL记录
- 格式 :字段名 数据类型 not null
##可连用:字段名 数据类型 not null default 值
- 默认约束(default)