数据库MySQL


#
# sql 支持的数据类型 MySQL 支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。 - int 整型 - DECIMAL 小数型   对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 - varchar 字符串类型 (变长字符串) - text 字符串类型 (长度大于varchar 长文本数据) - char 字符串类型 (长度小于 varchar 定长字符串) - datetime 日期  YYYY-MM-DD HH:MM:SS  混合日期和时间值

# 查询语句 select ## 简单的查询 - 语法 ```sql select 字段名1, 字段名2, ... from 表名; # * 代表查询所有的字段 select * from 表名; ``` - 案例 ```sql select name from admin; select name, pwd from admin; select * from admin; ``` ## where语句 牵扯到where语句,可以进行复杂的查询操作 - 语法 ```sql select 字段名1, 字段名2, ... from 表名 where 条件 ``` ### where 语句设计的运算 - 比较运算符 `>`   `<`   `=`   `!=`   `<>` - 关系运算符 `and`  `or` - in 关键字 `in(条件1, 条件2, ...)`
- 案例 ```sql select * from article where grade = '一等奖'; select * from article where id < 10; select * from article where id > 10 and id < 20; select * from article where id = 15 or grade = '一等奖' ; select * from article where grade != '一等奖'; select * from article where grade <> '一等奖'; select * from article where id in (10, 20, 30, 40); ```
## 关键字查询 like 语句 关键字查询也称为 模糊查询, 使用 like 关键字实现; keywords 是查询的关键字,可以使用 `%` 或者 `_` 进行修饰 > `%` : 匹配任意个字符; `_` : 一个`_`匹配任意一个字符; 可以同时使用多个 - 语法 ```sql select 字段名1, 字段名2, ... from 表名 where 字段名 like  keywords ``` - 案例 ```sql # 精准匹配 select * from article where title like '丽'; # 末尾匹配 select * from article where title like '%月'; # 包含 select * from article where title like '%的%'; # 首位匹配 select * from article where title like '一%'; # 匹配两个字符,第二个字符必须是 的 select * from article where title like '_的'; select * from article where author like '__月'; # 结合使用 select * from article where author like '__的%'; ```





# 插入语句 insert values 里边的值 和 前边的字段一一对应的, 也要注意 值的类型 - 语法 ```sql # 一次插入一条语句 insert into 表名 (字段名1, 字段名2, ...) values (value1, value2, ...); # 批量插入 insert into 表名 (字段名1, 字段名2, ...) values (value1, value2, ...),(value1, value2, ...), ... ; ``` - 案例 ```sql insert into admin (name, pwd) values ('真三', '123123'); insert into admin (name, pwd) values ('真三', '123123'), ('李四', '123555'), ('真三er', '123123'); ```
# 删除语句 delete - 语法 ```sql # 清空数据表 delete from 表名 ; # 删除指定条件的数据 delete from 表名 where 条件; ``` - 案例 ```sql # 清空数据表 delete from admin ; # 删除指定条件的数据 delete from 表名 where id = 3; ```
# 修改语句  update - 语法 ```sql # 修改表中对应字段的所有记录 update 表名 set 字段名1 = newValue1, 字段名1 = newValue1, ... ; # 修改指定条件对应的字段值 update 表名 set 字段名1 = newValue1, 字段名1 = newValue1, ... where 条件; ``` - 案例 ```sql update admin set name = '休闲' ; update admin set name = '东方' where id = 10; ```
# 排序 order by ORDER BY 关键字用于对结果集进行排序。用于对结果集按照一个列或者多个列进行排序。; ORDER BY 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 DESC 关键字 rules 排序规则: 默认是升序asc ,  desc 降序 - 语法 ```sql select * from 表名 order by 字段名1/rules, 字段名2/rules, ... ; ``` - 案例 ```sql # 默认是升序 select * from classify order by readNum ; # 降序排列 select * from classify order by readNum desc; # 指定多个排序字段, 第一个字段相同,则按照第二个字段进行排序,依次类推 select * from classify order by readNum asc, id desc; ``` # 分组  group by GROUP BY 语句可结合一些聚合函数来使用; GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。
- 语法 ```sql select 字段名1, 字段名2, ..., count(字段名) from 表名 group by  字段名; ``` - 案例 ```sql select kind_list, count(id) from classify group by kind_list; ``` # 分页 limit
- 语法 ``` # 获取指定长度length的数据 select * from 表名 limit length; # 从指定的索引值位置开始 获取指定长度的数据  索引值从0开始 select * from 表名 limit startIndex, length; ``` - 案例 ```sql select * from classify limit 10; select * from classify limit 10, 10; select * from classify limit 20, 10; select * from classify limit 0, 10; ``` - 分页的SQL 把 页码 page 作为一个变量 ``` select * from 表名 limit ${(page - 1 )* length}, length; ``` # 函数 - count(字段) :函数返回匹配指定条件的行数 - sum() : 求和 函数返回数值列的总数。 - max() : 最大值  函数返回指定列的最大值。 - min() : 最小值  函数返回指定列的最小值。 - avg() : 平均值  函数返回数值列的平均值。 ```sql select count(id), max(score), min(score), sum(score), avg(score) from user; ``` # 指定别名 通过 `as` 关键字指定别名, `as` 关键字可以省略 ## 对字段指定别名 ```sql select count(id), max(score), min(score), sum(score), avg(score) from user; select count(id) '总数', max(score) '最大值', min(score), sum(score) '总分', avg(score) from user; select count(id) '总数', max(score) '最大值', min(score) as '最小值', sum(score) '总分', avg(score) as '平均值' from user; ```
## 对表指定别名 通过 `as` 关键字指定别名, `as` 关键字可以省略 ```sql select b.name, a.c_name, a.grade from score a , student b where b.name = '李四' and b.id = a.stu_id; ```
# 多表查询 - 把一个表的查询结果作为另一个表的查询条件 ```sql select c_name, grade from score where stu_id = (select id from student where name = '李四') ``` - 等值链接 ```sql select b.name, a.c_name, a.grade from score a , student b where b.name = '李四' and b.id = a.stu_id; ```