MySQL Day02

5.7 分组查询

select fieldName
from tbName 
where condition_ 
group by 分组要求;

-- group by是一个分组关键字
-- 查询各部门人数是多少
-- 1. 需要按照department_id进行分组
-- 2. 计数需要使用count, 根据用户的employee_id进行计数操作
select department_id, count(employee_id)
from t_employees
group by department_id;

-- 查询各部门的平均工资
-- 1. 需要按照department_id进行分组
-- 2. 平均工资使用avg方法计算
select department_id, avg(salary)
from t_employees
group by department_id;

-- 查询各部门,各岗位的人数
-- 1. 需要按照department_id进行分组
-- 2. 需要按照岗位job_id进行分组
-- 3. 记录人数,count(employee_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
-- 如果使用group by要求分组字段一定是查询要求字段,这里需要根据查询结果进行分组
select department_id
from t_employees
group by job_id;

5.8 分组过滤查询

select fieldName
from tbName 
where condition_ 
group by 分组要求
having 过滤规则;
-- having是在 group by 之后的条件过滤

-- 查询指定100,50,30,80最高工资
-- 1. 需要按照department_id进行分组
-- 2. 最高工资
-- 3. 限制manager_id = 100
-- 4. 限制department_id号为100,50,30,80
select department_id, max(salary)
from t_employees
where manager_id = 100
group by department_id
having department_id in (100, 50, 30, 80);

5.9 限定查询

select fieldName
from tbName
limit 限制;
-- limit [offset_start], row_count

-- 查询员工表中前10个数据,员工first_name, employee_id
select employee_id, first_name
from t_employees
limit 10;

-- 查询员工表中10个数据,要求offset为3,员工first_name, employee_id
-- 起始行从0开始
select employee_id, first_name
from t_employees
limit 3,10;

-- 【重点】
-- limit核心用法,分页查询
-- pageCount 当前是第几页
-- itemCount 一页展示多少个元素
-- select * from tbName limit (pageCount - 1) * itemCount, itemCount;

-- 展示第一页10条数据
select employee_id, first_name
from t_employees
limit 0, 10;

-- 展示第二页10条数据
select employee_id, first_name
from t_employees
limit 10, 10;

-- 展示第三页10条数据
select employee_id, first_name
from t_employees
limit 20, 10;

5.10 子查询[重点,难点]

5.10.1 基本格式

select fieldName
from tbName
where (子查询结果);

5.10.2 子查询结果作为条件判断约束

-- 查询工资高于Jack的员工id和姓名
-- 1. 找出Jack的工资
-- 2. 得到Jack工资,作为条件查询对应的员工信息
select salary
from t_employees
where first_name = 'Jack';
select employee_id, first_name
from t_employees
where salary > 8400;

-- 整合为子查询
-- 条件判断
select employee_id, first_name
from t_employees
where salary > (select salary
                from t_employees
                where first_name = 'Jack');

5.10.3 子查询结果作为枚举限制 in

-- 查询和Jack同部门的员工信息
-- 1. 找出Jack的部门编号
select department_id
from t_employees
where first_name = 'Jack';

-- 2. 根据Jack的部门编号,使用in枚举查询,限制条件
select employee_id, first_name
from t_employees
where department_id in (80);

-- 整合为子查询
select employee_id, first_name
from t_employees
where department_id in (select department_id
                        from t_employees
                        where first_name = 'Jack');

5.10.4 子查询结果作为一张表,从表内查询指定数据

-- 查询员工表中工资前五名的员工信息
-- 1. 找到员工的id,first_name,工资降序
select employee_id, first_name
from t_employees
order by salary desc;

select employee_id, first_name
from (select employee_id, first_name
      from t_employees
      order by salary desc) as temp
limit 5;

5.11 合并查询[仅了解]

-- 合并,要求查询要求的字段个数一致
-- 去重
select employee_id, first_name
from t_employees
union
select job_id, job_title
from t_jobs;

-- 合并,要求查询要求的字段个数一致
-- 不去重
select department_id, first_name
from t_employees
union all
select manager_id, department_name
from t_departments;

5.12 表连接查询【重点】

5.12.1 基本格式

select fieldName
from tbName1
连接符 tbName2
on 条件

5.12.2 笛卡尔乘积【避免】

-- 笛卡尔乘积,没有约束条件,数据库匹配发生相乘关系,结果也不是预期结果
-- 无意义结果
select employee_id, first_name
from t_employees
inner join t_jobs;

5.12.3 内连接查询 inner join on 两张表

-- 查询所有部门部门名,和对应的员工信息id和first_name
select t_departments.department_name,
       t_employees.employee_id,
       t_employees.first_name -- 查询内容
from t_employees -- 从员工表中查询
         inner join t_departments -- 内连接部门表
                    on t_employees.department_id = t_departments.department_id;
-- 条件限制员工表中的部门Id = 部门表中的部门id

-- 查询所有部门部门名,和对应的员工信息id和first_name
-- 给予表格一个别名,方便使用
select d.department_name,
       e.employee_id,
       e.first_name -- 查询内容
from t_employees e-- 从员工表中查询
         inner join t_departments d-- 内连接部门表
                    on e.department_id = d.department_id; -- 条件限制员工表中的部门Id = 部门表中的部门id

5.12.4 左外连接 left join on

-- 左外连接 左表是主表,要求左表完整显示,右表匹配左表数据,如果右表没有数据匹配,显示null
-- 查询所有的员工信息ID,first_ame,已经对应的部门名字
select te.employee_id, te.first_name, td.department_name
from t_employees te
         left join t_departments td on te.department_id = td.department_id;

5.12.5 右外连接 right join on

-- 右外连接查询,右表是主表,要求右表完整展示,左表匹配右表数据,如果左表没有数据匹配,显示null
-- 查询所有部门对应员工信息,员工信息没有显示null
select td.department_name,te.employee_id, te.first_name
from t_employees te
         right join t_departments td on te.department_id = td.department_id;

六 DML语句

6.1 增加 insert

create table person(
    id int,
    name varchar(30),
    salary float(8, 2),
    age int
);

-- 规矩插入
insert into person(id, name, salary, age) VALUE (1, '骚磊', 100.5, 16);

-- 省略所有的字段名,按照字段顺序添加
insert into person value (2, '茂林', 20.5, 50);

-- 指定字段数据插入
insert into person(name, age) value ('骚杰', 66);

-- 禁止字段数据类型和插入数据类型不一致!!!
insert into person(name, age) value ('66', '23333333');
insert into person(name, age) value ('66', '你在整一个试试???');

6.2 修改 update 【慎用】

-- Unsafe query: 'Update' statement without 'where' updates all table rows at once
-- 没有任何约束的情况下,当前指定数据表中的所有数据行全部执行修改操作
-- 一定要带有where条件约束
update person set id = 10;
-- OK
update person set id = 10 where name = '骚磊';
-- 可以同时修改多个数据,不同的字段赋值操作使用 逗号隔开
update person set id = 20, age = 10, salary = 200000.01 where name = '骚磊';

6.3 删除 delete【慎用】

-- Unsafe query: 'Delete' statement without 'where' clears all data in the table
delete from person;
-- 根据条件约束删除是允许的
delete from person where name = '66';

6.4 truncate 清空整表数据

-- 清空整表数据,并且会影响到一定数据约束,例如auto_increment 自增长
truncate table person;

七 库表操作

7.1 创建数据库

-- dbName数据库的名字,可以约束编码集[是情况而定]
create database dbName [character set charset];

7.2 删除数据库

-- 删除指定数据库
drop database dbName;

7.3 修改数据库

-- 修改数据库编码集
alter database dbname character set utf8;

7.4 数据类型

7.4.1 数值类型

MySql统计每个部门中符合条件的人数 mysql查询部门人数_数据库

7.4.2 日期类型

MySql统计每个部门中符合条件的人数 mysql查询部门人数_子查询_02

7.4.3 字符串类型

MySql统计每个部门中符合条件的人数 mysql查询部门人数_数据库_03

7.5 创建表

-- 1. 选择使用的数据库
create table tbName
(
    fieldName1 fieldType1,
    fieldName2 fieldType2,  
    fieldName3 fieldType3,  
    fieldName4 fieldType4
);
-- 可以加入约束,字符集和存储引擎

八,数据约束

8.1 默认值 default

MySql统计每个部门中符合条件的人数 mysql查询部门人数_MySql统计每个部门中符合条件的人数_04

8.2 非空 not null

MySql统计每个部门中符合条件的人数 mysql查询部门人数_数据库_05

8.3 唯一 unique

MySql统计每个部门中符合条件的人数 mysql查询部门人数_MySql统计每个部门中符合条件的人数_06

8.4 主键 primary key

MySql统计每个部门中符合条件的人数 mysql查询部门人数_数据库_07

8.5 自增长 auto_increment

MySql统计每个部门中符合条件的人数 mysql查询部门人数_子查询_08