2. 非关系型数据库
MongoDB Redis[重点]
使用哈希表结构进行数据存储,数据形式 Key-Value 键值对模型
4. 数据库管理系统 DBMS
数据库管理系统:
Database Management System
DBMS
操作,管理数据库大型软件,用于管理,创建,使用和查询数据操作。
目前市面上流行的数据库
Oracle
甲骨文公司主要产品!!!核心产品
稳定,安全,复杂,容量大。大型项目都会使用 Oracle 数据库 阿里巴巴 腾讯 抖音
【贵】
DB2
IBM公司,目前不多见了
SQL Server
MS(微软/巨硬) 公司,关系型数据库,在国内还是有很大市场的
SQLite
轻量级数据库,原码 1W C语言。大量用于手机通讯录!!!
5. MySQL 介绍
MySQL默认编码集是 Latin1 ==> 西欧,MySQL起源于西欧,瑞典公司研发的数据库软件 AB 公司。
AB公司研发的 MySQL 数据库在刚刚起步阶段,免费开源。
【社区】
社区的大牛,各方力量开始融入MySQL,各式各样的插件,各种各样辅助。
InnoDB 跨时代的存储引擎。
MySQL性能还是非常不错的!!!
大中小企业使用 MySQL 数据都是完全 OK 的。
美国小鹰号常规动力航母数据管理使用的就是 MySQL 数据库
MySQL 隶属于 Oracle 公司,Oracle 目前维护的版本 5.5 5.6 5.7 8.0
8.0 性能 5.7 两倍!!!
实际开发中,需要考虑项目的稳定性,兼容性,普适性
6. SQL
6.1 SQL 语句概述
SQL语句是数据库通用操作语句(查询,创建,删除,修改...),不管是 MySQL, Oracle, SQL Server 都是支持 SQL 语句。数据库语句分类
C Create
R Read 【重点 select 查询】
U Update
D Delete
核心:
数据库设计
数据库查询语句
6.2 SQL 基本操作
# 数据库连接操作
# cmd > 命令在 终端/命令提示符操作
cmd > mysql -hlocalhost -uroot -p123456
# mysql 明确告知当前操作连接的数据库是哪一个
# -h host 数据库所在 ip地址,域名,主机名,后期会连接远端/服务器端数据库
# localhost 本地域名/主机名 127.0.0.1 通常情况下如果连接的是本机数据库,可以省略 localhost
# -uroot -u user 用户 root 是用户名
# -p password 密码 对应当前用户的密码
# 常用连接方式
cmd > mysql -uroot -p
Enter password: ******
# 展示当前数据库服务器中有多少个数据库
show databases;
# 创建数据库
# create database dbName;
create database javaee_2203;
# Query OK, 1 row affected (0.00 sec)
# Query OK, 当前 SQL 语句目标执行 OK,1 row affected 当前数据库有一行受到影响。
# 展示创建数据库过程中一些信
show create database javaee_2203;
# +-------------+----------------------------------------------------------------------+
# | Database | Create Database |
# +-------------+----------------------------------------------------------------------+
# | javaee_2203 | CREATE DATABASE `javaee_2203` /*!40100 DEFAULT CHARACTER SET utf8 */ |
# +-------------+----------------------------------------------------------------------+
# DEFAULT CHARACTER SET utf8 当前 javaee_2203 数据库对应编码集为 UTF-8
# 删除数据库【慎用,而且一般情况下,你没有权限删除】
drop database javaee_2203;
# 创建数据库过程中可以选择编码集
create database javaee_2301 character set gbk;
create database javaee_2302 character set latin1;
# 修改已存在数据库对应编码集
alter database javaee_2301 character set utf8;
# 选择使用哪一个数据库键操作,后期的针对于【数据表的操作】内容是在指定数据库
use javaee_2203;
# 查询当前数据库服务器操作的数据库是哪一个
select database();
7. 表结构操作
7.1 创建数据表
-- 基本格式
create table tbName
(
字段名1 数据类型 约束操作,
字段名2 数据类型 约束操作,
字段名3 数据类型 约束操作
);
use javaee_2203;
create table person
(
# 字段(field)名为 id,字段对应的数据类型为 int 类型 对应 Java中的 int 类型
id int,
# 字段(field)名为 name,数据类型是 varchar 类型。MySQL 数据库可变长字符串,最大允许的字符个数为32
# 字符个数为 5,MySQL 给予当前数据分配的字符个数为 5。对应 Java 中的 String 类型
name varchar(32),
# 字段(field)名为 age,数据类型为 int 类型
age int,
# 字段(field)名为 gender,数据类型为 tinyint ==> Java 中 boolean or byte
gender tinyint
);
7.2 查看当前表结构信
# 当前操作的数据库中有多少张数据表
show tables;
# +-----------------------+
# | Tables_in_javaee_2203 |
# +-----------------------+
# | person |
# +-----------------------+
# 查看指定表结构信
desc tabName;
desc person;
# +--------+-------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +--------+-------------+------+-----+---------+-------+
# | id | int(11) | YES | | NULL | |
# | name | varchar(32) | YES | | NULL | |
# | age | int(11) | YES | | NULL | |
# | gender | tinyint(4) | YES | | NULL | |
# +--------+-------------+------+-----+---------+-------+
7.3 修改数据表
7.3.1 添加字段
# 默认在整个数据表的最后
alter table person add information text;
# +-------------+-------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +-------------+-------------+------+-----+---------+-------+
# | id | int(11) | YES | | NULL | |
# | name | varchar(32) | YES | | NULL | |
# | age | int(11) | YES | | NULL | |
# | gender | tinyint(4) | YES | | NULL | |
# | information | text | YES | | NULL | |
# +-------------+-------------+------+-----+---------+-------+
# 可以指定在某一个字段之后添加新的字段
# 在 gender 字段之后,添加 salary 字段,要求数据类型为 float(8, 2)
# float(8, 2) 要求数据的总位数最大为 8 位,且保留两位小数 -999999.99 ~ 999999.99
alter table person add salary float(8, 2) after gender;
# +-------------+-------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +-------------+-------------+------+-----+---------+-------+
# | id | int(11) | YES | | NULL | |
# | name | varchar(32) | YES | | NULL | |
# | age | int(11) | YES | | NULL | |
# | gender | tinyint(4) | YES | | NULL | |
# | salary | float(8,2) | YES | | NULL | |
# | information | text | YES | | NULL | |
# +-------------+-------------+------+-----+---------+-------+
7.3.2 修改字段数据类型
# 修改 person 表中的字段 name 数据类型为 varchar(256)
alter table person modify name varchar(256);
# | name | varchar(32)
# 仅修改数据类型
# | name | varchar(256)
7.3.3 修改字段名称和数据类型
# 修改 person 表中 information 字段改为 description 同时数据类型改为 varchar(256)
alter table person change information description varchar(256);
# | information | text
# 修改字段名和数据类型之后
# | description | varchar(256)
7.3.4 删除指定字段
# 删除 person 表中 description 字段
alter table person drop description;
7.3.5 修改数据表名称
# 重命名数据表名 将原本的 person 表改为 people 表
rename table person to people;
rename table people to person;
7.4 删除数据表【慎用】
# 删除指定名称的数据表
drop table tbName;
drop table person;
8. 添加数据 insert
# +--------+--------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +--------+--------------+------+-----+---------+-------+
# | id | int(11) | YES | | NULL | |
# | name | varchar(256) | YES | | NULL | |
# | age | int(11) | YES | | NULL | |
# | gender | tinyint(4) | YES | | NULL | |
# | salary | float(8,2) | YES | | NULL | |
# +--------+--------------+------+-----+---------+-------+
# 1. 规规矩矩,有啥是啥
# SQL 语句中字符串使用的都是 英文单引号 '' 包含!!!
insert into
person(id, name, age, gender, salary)
values(1, '苟磊', 16, 0, 10000.55);
# 2. 指哪打哪
insert into
person(id, name, age)
values(2, '王乾', 66);
# 3. 规规矩矩偷懒,可以省略 into 可以省略表名之后所有字段声明,但是必须按照表结构字段顺序,数据类型提供对应的
# 具体数据。
insert
person
values(3, '王迟平', 76, 0, -500);
insert
person
values('王迟平', 76, 0);
# ERROR 1136 (21S01): Column count doesn't match value count at row 1
# 错误情况,给予当前数据表的数据情况和表结构要求的数据数量不匹配
insert into person values(4, '栋哥', 36, 0, 2000);
insert into person values(5, '杨仔', 30, 0, 1000);
insert into person values(6, '熊大', 60, 0, 5000);
insert into person values(7, '博哥', 35, 0, 9000);
insert into person values(8, '高老师', 35, 0, 5000);
insert into person values(9, '彭于晏', 36, 0, 999000);
insert into person values(10, '吴彦祖', 40, 0, 100000);
insert into person values(11, '光头强', 55, 0, -5);
insert into person values(12, '王冰冰', 18, 1, 19000);
insert into person values(13, '李沁', 21, 1, 129000);
insert into person values(14, '鞠婧祎', 22, 1, 4000);
insert into person values(15, '高圆圆', 35, 1, 6000);
insert into person values(16, '张天爱', 30, 1, 19000);
insert into person values(17, '迪丽热巴', 28, 1, 91000);
insert into person values(18, '古力娜扎', 29, 1, 92000);
insert into person values(19, '欧阳娜娜', 25, 1, 9999);
insert into person values(20, '周淑怡', 29, 1, 50);
9. 修改数据 update 【慎用】
# 没有任何约束的修改操作有可能会所有的数据行全部修改。导致数据错误!!!
update person set name = '王迟平';
# Query OK, 19 rows affected (0.00 sec)
# Rows matched: 20 Changed: 19 Warnings: 0
# 执行操作成功,19行受到了影响
# 匹配行数:20 更新/更换:19 警告:0
# 有效修改,约束条件修改
# 修改数据表 person 中 id 为 3 的数据行其中的 name 字段数据改为'王总迟平'
update person set name = '王总迟平' where id = 3;
# Query OK, 1 row affected (0.00 sec)
# Rows matched: 1 Changed: 1 Warnings: 0
# 执行操作成功,1行受到了影响
# 匹配行数:1 更新/更换:1 警告:0
10. 删除数据 delete 【慎用】
# 未加入任何的条件限制,当表所有数据行被删除
delete from person;
# Query OK, 20 rows affected (0.00 sec)
# 执行操作成功,20 行受到了影响
# 有效修改,约束条件删除
delete from person where id = 10;
# Query OK, 1 row affected (0.00 sec)
# 执行操作成功,1 行受到了影响
11. 事务操作
# 关闭自动提交 SQL 语句,开启事务操作
set autocommit = 0;
# 开启自动提交 SQL 语句,关闭事务操作
set autocommit = 1; # MySQL 数据库默认状态
# 从 set autocommit = 0; 或者 commit 操作之后 到 rollback 所有 SQL 语句操作撤销。【回滚】
# 【不会终止事务 不会开启自动提交】
rollback;
# 从 set autocommit = 0; 或者 rollback 操作之后 到 commit 所有 SQL 语句确定执行,手动提交。
# 【不会终止事务 不会开启自动提交】
commit;
12. 查询操作
12.1 基本格式
select 查询内容 -- 明确当前查询目标字段内容
from
从哪里查; -- 数据来自哪一张表
12.1.1 基本格式 字段 和 表名
# * 表示任意字段, from person 会从整个 person 中找出所有数据行,所有字段名称,没有任何的限制
# 数据量小还行,但是如果是一个特别恐怖的数据表。。。。效率会极低。
select * from person;
# 指定字段名和数据表名查询目标数据
select id, name from person;
select id, name, salary from person;
select name, gender, salary from person;
12.1.2 基本格式 字段别名
# 在字段之后使用 as 后面是当前字段别名 使用不多
select name as '姓名', gender as '性别', salary as '工资' from person;
12.1.3 计算字段数据结果
# 取出某一个字段对应的数据通过计算操作得到其他内容
select name as '姓名', gender as '性别', salary * 12 as '年薪' from person;
12.1.4 去重 distinct
# 相同数据仅保留一个,去重查询
select distinct age from person;
12.2 排序查询 order by
排序规则效果
asc
升序排序【缺省属性】
desc
降序排序
12.2.1 单一条件排序
# 查询目标字段为 员工 id号,名字,工资,并且按照工资进行【升序】排序
select employee_id, first_name, salary
from t_employees
order by salary asc;
select employee_id, first_name, salary
from t_employees
order by salary; # asc 可以省略, order by 默认就是按照指定的字段进行升序排序
# 查询目标字段为 员工 id号,名字,工资,并且按照工资进行【降序】排序
select employee_id, first_name, salary
from t_employees
order by salary desc;
12.2.2 多条件排序
# 查询目标字段为 员工 id号,名字,工资,首先按照工资【升序】排序,如果工资一致按照工号【降序】排序
select employee_id, first_name, salary
from t_employees
order by salary, employee_id desc;
12.3 条件查询 where
12.3.1 基本格式
select fieldName # 查询目标的字段名称
from tbName # 明确字段对应的数据表
where condition; # 查询所约束的条件,条件是一个 boolean 类型 true or false
12.3.2 = 等值判断
# Java 中使用的 == or equals 方法来完成等值判断
# 数据库中使用的 = 在条件约束中来完成等值判断
# 找出员工 id,名字,工资,要求工资 = 10000 元
select employee_id, first_name, salary -- 明确字段
from t_employees -- 字段对应的数据表
where salary = 10000; -- 限制的 = 判断条件
# 找出员工 id,名字, 工资 要求 manager_id = 100
# 查询的目标字段是没有 manager_id 字段,但是在查询条件约束中可以使用 manager_id
# 目前查询对应【数据源】中有对应字段存在的。
select employee_id, first_name, salary
from t_employees
where manager_id = 100;
select employee_id, first_name, salary, manager_id
from t_employees
where manager_id = 100;
12.3.3 不等值判断( > < >= <= != <>)
# 【补充说明】
# <> SQL 当中的不等于,早期用法,后期允许使用 !=
# 阅读性 != 更好
# 如果是考虑老版本的 SQL ,<> 移植性更好
# 查询员工 id,名字,部门id号, 要求工资 大于 10000 rows:15
select employee_id, first_name, department_id
from t_employees
where salary > 10000;
# 查询员工 id,名字,部门id号, 要求工资 小于 3000 rows:24
select employee_id, first_name, department_id
from t_employees
where salary < 3000;
# 查询员工 id,名字,部门id号, 要求工资 大于等于 10000 rows:19
select employee_id, first_name, department_id
from t_employees
where salary >= 10000;
# 查询员工 id,名字,部门id号, 要求工资 小于等于 3000 rows:26
select employee_id, first_name, department_id
from t_employees
where salary <= 3000;
# 查询员工 id,名字,部门id号, 要求工资 不等于 10000
select employee_id, first_name, department_id
from t_employees
where salary != 10000; # 阅读性更好,和开发语言要求一致 Java C C++ PHP JS
select employee_id, first_name, department_id
from t_employees
where salary <> 10000; # 移植性更好,适配各版本 SQL
12.3.4 逻辑判断(and &&, or ||, not !)
# 查询员工 id,名字,部门id号,salary 要求部门id 为80 并且工资大于 10000
select employee_id, first_name, department_id, salary
from t_employees
where department_id = 80 and salary > 10000;
# 查询员工 id,名字,部门id号,salary 要求部门id 为80 并且工资大于 10000
select employee_id, first_name, department_id, salary
from t_employees
where department_id = 80 && salary > 10000;
# 查询员工 id,名字,部门id号,salary 要求部门id 为80 或者 工资大于 10000
select employee_id, first_name, department_id, salary
from t_employees
where department_id = 80 or salary > 10000;
# 查询员工 id,名字,部门id号,salary 要求部门id 为80 或者 工资大于 10000
select employee_id, first_name, department_id, salary
from t_employees
where department_id = 80 || salary > 10000;
# 查询员工 id,名字,部门id号,salary 要求部门id 不为 80
select employee_id, first_name, department_id, salary
from t_employees
where department_id != 80; # 正确
# select employee_id, first_name, department_id, salary
# from t_employees
# where department_id not = 80; # 语法报错,不允许这样完成
select employee_id, first_name, department_id, salary
from t_employees
where not department_id = 80; # 正确
select employee_id, first_name, department_id, salary
from t_employees
where ! department_id = 80; # 语法不报错,但是没有结果 not 和 ! 不能直接替换
12.3.5 区间判断(between and)
# 查询员工id,名字,工资,要求工资在 5000 ~ 10000 之间
# where + and 方式 ,逻辑表达式拼接
select employee_id, first_name, salary
from t_employees
where salary >= 5000 && salary <= 10000;
# between 5000 and 10000; 完成一个闭区间限制 5000 <= salary <= 10000;
#【要求】 between 5000 and 10000; 必须前小后大
select employee_id, first_name, salary
from t_employees
where salary between 5000 and 10000;
# where + and 方式 ,逻辑表达式拼接
select employee_id, first_name, salary
from t_employees
where salary <= 10000 && salary >= 5000;
# between 5000 and 10000; 完成一个闭区间限制 5000 <= salary <= 10000;
# 如果 between and 限制条件,前大后小 无数据产出
select employee_id, first_name, salary
from t_employees
where salary between 10000 and 5000;
12.3.6 Null值判断
# is null 判断指定字段数据对应的内容是否为 null ,如果是返回 true 如果不是返回 false
# is not null 判断指定字段数据对应的是否不为 null,如果不是 返回 true 如果是 返回 false
# 查询员工id,名字,提成,工资,找出提成不为 null 的
select employee_id, first_name, commission_pct, salary
from t_employees
where commission_pct is not null;
# 查询员工id,名字,提成,工资,找出提成为 null 的
select employee_id, first_name, commission_pct, salary
from t_employees
where commission_pct is null;
12.3.7 枚举查询 in
# in (数据值1, 数据值2) 根据指定的数据值1, 数据值2 匹配对应的数据行内容
# not in (数据值1, 数据值2) 根据指定的数据值1, 数据值2 找出不匹配对应的数据行内容
# 查询员工id,名字,工资,要求部门id 为 80 和 50
# in 查询效率偏低,在 SQL 优化过程中,会考虑将 in 替换成其他语句。
select employee_id, first_name, salary
from t_employees
where department_id in (50, 80);
select employee_id, first_name, salary
from t_employees
where department_id = 50 || department_id = 80;
# 查询员工id,名字,工资,要求部门id 不为 80 和 50
select employee_id, first_name, salary
from t_employees
where department_id not in (50, 80);
select employee_id, first_name, salary
from t_employees
where department_id != 50 && department_id != 80;
select employee_id, first_name, salary
from t_employees
where not (department_id = 50 || department_id = 80);
12.3.8 模糊查询 like
# % 匹配任意个数字符
# 案例 %a% 要求当前查询匹配规则中必须有 a 字符 a 之前不限制 a 之后也不限制
# _ 表示匹配一个字符
# 案例 _a__ 要求当前查询匹配规则中 a 之前必须有一个字符,a之后必须有两个字符
# 查询员工 id ,名字,工资,要求 名字中必须包含 V 【不区分大小写】
select employee_id, first_name, salary
from t_employees
where first_name like '%V%';
# 查询员工 id ,名字,工资,要求 名字中要求 V 开头【不区分大小写】
select employee_id, first_name, salary
from t_employees
where first_name like 'V%';
# 查询员工 id ,名字,工资,要求 名字中要求 n 结尾【不区分大小写】
select employee_id, first_name, salary
from t_employees
where first_name like '%n';
# 查询员工 id ,名字,工资,要求 名字中要求 a 在第二个字符
select employee_id, first_name, salary
from t_employees
where first_name like '_a%';
# 查询员工 id ,名字,工资,要求 名字中要求 a 在第二个字符,a 之后有 4 个字符 字符总长在 6 个
select employee_id, first_name, salary
from t_employees
where first_name like '_a____';
12.3.9 分支结构查询
# case
# when condition1 then ret1
# when condition2 then ret2
# when condition3 then ret3
# end as '别名'
# 分支结构从 case 开始,到 end 结束,满足一个条件对应一个结果,类似于 Java 中 switch case
# 查询员工id,名字和工资等级
# >= 10000 A
# >= 8000 B
# >= 6000 C
# >= 4000 D
# else E
# 类似于 switch case 或者是 if - else if 结构,条件罗列需要注意细节和约束
select employee_id,
first_name,
salary,
case
when salary >= 10000 then 'A'
when salary >= 8000 then 'B'
when salary >= 6000 then 'C'
when salary >= 4000 then 'D'
else 'E'
end as 'level'
from t_employees;
# 不推荐方式
select employee_id,
first_name,
salary,
case
when salary < 4000 then 'E'
when salary >= 4000 and salary < 6000 then 'D'
when salary >= 6000 and salary < 8000 then 'C'
when salary >= 8000 and salary < 10000 then 'B'
when salary >= 10000 then 'A'
end as 'level'
from t_employees;
12.4 分组查询
# select fieldName
# from tbName
# where condition
# group by 分组条件;
# 查询每一个部门有多少人
# 1. 分组条件是 部门编号 例如 按照 30 分组,
# 2. 分组之后可以计数 部门员工的 id 号,得到部门人数
select department_id, count(employee_id)
from t_employees
group by department_id;
# 查询每一个部门有平均工资
# 1. 分组条件是 部门编号 例如 按照 30 分组,
# 2. 分组之后按照部门进行 salary 平均数计算
select department_id, avg(salary)
from t_employees
group by department_id;
# 查询每一部门,每一个岗位有多少人
# 1. 首先按照部门分组
# 2. 按照岗位分组
# 3. count 计数员工 id
select department_id, job_id, count(employee_id)
from t_employees
group by department_id, job_id;
# [42000][1055] Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
# column 'company.t_employees.department_id' which is not functionally dependent on columns in GROUP BY clause;
# this is incompatible with sql_mode=only_full_group_by
# 分组对应的字段必须在查询目标字段中存在,如果没有执行报错!!!
# 分组数据源是【查询字段】
# 分组操作是在查询操作之后完成的!!!
select department_id
from t_employees
group by job_id;
12.5 分组过滤查询
# select fieldName
# from tbName
# where condition
# group by 分组条件
# having 分组之后结果限制/条件;
# having 是在分组之后使用的条件约束。
# 查询部门id 在 100, 50. 80的最高工资
# 1. 需要按照部门 ID 分组
# 2. 最高工资
# 3. 分组之后需要将部门限制在 100 50 80
select department_id, max(salary)
from t_employees
group by department_id -- 部门分组
having department_id in (100, 50, 80); -- 分组之后的条件 限制在部门 id 100 50 80
# 查询部门人数大于 20 的部门
# 1. 按照部门分组
# 2. 统计人数
# 3. 限制条件为 人数大于 20 的
select department_id, count(employee_id)
from t_employees
group by department_id -- 部门分组
having count(employee_id) > 20; -- 限制条件为部门人数 大于 20
# 查询部门员工工资高于 5000 的,并且部门人数大于 20 的部门数据
# 1. 限制工资大于 5000
# 2. 按照部门分组
# 3. 统计各部门人数
# 4. 分组之后的部门人数 大于 20 作为约束条件
select department_id, count(employee_id)
from t_employees
where salary > 5000 -- 限制查询数据结果工资在 5000 以上 可以有效降低分组数据行数
group by department_id -- 按照部门分组
having count(employee_id) > 20 -- 根据部门的人数限制部门人数在 20 以上;
12.6 限定查询
# limit
# select fieldName
# from tbName
# limit 限制;
# 查询数据表中前十的员工信,员工ID和名字
select employee_id, first_name
from t_employees
limit 10;
# 查询数据中首先按照工资降序排序,找出工资前十的员工信,员工ID,名字和工资
# 1. 首先需要进行排序操作,按照工资降序
# 2. 排序之后的结果,我们找出前十员工
select employee_id, first_name, salary
from t_employees
order by salary desc
limit 10;
# 查询数据表中的员工ID,名字,从下标 5 开始查询,查询的数据行数为 10 行
select employee_id, first_name
from t_employees
limit 5, 10; -- limit offset, length/count
# 显示从 0 下标开始,10条数据
select employee_id, first_name
from t_employees
limit 0, 10;
# 显示从 10 下标开始,10条数据
select employee_id, first_name
from t_employees
limit 10, 10;
# 显示从 20 下标开始,10条数据
select employee_id, first_name
from t_employees
limit 20, 10;
# pageCount 页码 1 2 3 4 5 or 0 1 2 3 4
# rowsCount 每一页数据行数 10 15 20
# 公式
# 如果 pageCount 从 1 开始
# limit (pageCount - 1) * rowsCount, rowsCount
12.7 内置函数数学相关
方法功能描述
sum()
查询数据行指定字段所有数据之和
avg();
查询数据行指定字段所有数据的平均数
count();
查询数据行指定字段所有数据个数
max();
查询数据行指定字段所有数据最大值
min();
查询数据行指定字段所有数据最小值
# count
# 查询各部门员工人数
select department_id, count(employee_id)
from t_employees
group by department_id;
# 找出部门为 60 的员工人数
select department_id, count(employee_id)
from t_employees
where department_id = 60;
# 全公司人数
select count(employee_id)
from t_employees;
# avg
# 全公司的平均工资
select avg(salary)
from t_employees;
# 找出 manager_id = 100 的员工平均工资
select avg(salary)
from t_employees
where manager_id = 100;
# 找出各个部门的平均工资
select department_id, avg(salary)
from t_employees
group by department_id;
# sum
# 查询各部门工资支出
select department_id as '部门编号', sum(salary) * 12 as '年支出'
from t_employees
group by department_id;
# 公司月人力支出
select sum(salary)
from t_employees;
# max
# 公司中工资最高的员工信
select employee_id, first_name, salary
from t_employees
where salary = (
select max(salary)
from t_employees
);
# min
# 公司中工资最低的员工信
select employee_id, first_name, salary
from t_employees
where salary = (
select min(salary)
from t_employees
);
12.8 字符串函数