• 二阶段day4


    查询语句

    建表如下:

    -- 如果存在就删除名为hrs的数据库
    drop database if exists `hrs`;
    
    -- 创建名为hrs的数据库并指定默认的字符集
    create database `hrs` default charset utf8mb4;
    
    -- 切换到hrs数据库
    use `hrs`;
    
    -- 创建部门表
    create table `tb_dept`
    (
    `dno` int not null comment '编号',
    `dname` varchar(10) not null comment '名称',
    `dloc` varchar(20) not null comment '所在地',
    primary key (`dno`)
    );
    
    -- 插入4个部门
    insert into `tb_dept` values 
        (10, '会计部', '北京'),
        (20, '研发部', '成都'),
        (30, '销售部', '重庆'),
        (40, '运维部', '深圳');
    
    -- 创建员工表
    create table `tb_emp`
    (
    `eno` int not null comment '员工编号',
    `ename` varchar(20) not null comment '员工姓名',
    `job` varchar(20) not null comment '员工职位',
    `mgr` int comment '主管编号',
    `sal` int not null comment '员工月薪',
    `comm` int comment '每月补贴',
    `dno` int not null comment '所在部门编号',
    primary key (`eno`),
    constraint `fk_emp_mgr` foreign key (`mgr`) references tb_emp (`eno`),
    constraint `fk_emp_dno` foreign key (`dno`) references tb_dept (`dno`)
    );
    
    -- 插入14个员工
    insert into `tb_emp` values 
        (7800, '张三丰', '总裁', null, 9000, 1200, 20),
        (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
        (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
        (3211, '张无忌', '程序员', 2056, 3200, null, 20),
        (3233, '丘处机', '程序员', 2056, 3400, null, 20),
        (3251, '张翠山', '程序员', 2056, 4000, null, 20),
        (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
        (5234, '郭靖', '出纳', 5566, 2000, null, 10),
        (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
        (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
        (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
        (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
        (3577, '杨过', '会计', 5566, 2200, null, 10),
        (3588, '朱九真', '会计', 5566, 2500, null, 10);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    建立完成,对该表做如下操作

    1查询员工及其主管的姓名

    select t1.ename as 员工
    	,t2.ename as 主管
    from tb_emp as t1 left join tb_emp as t2
    on t1.mgr = t2.eno;
    
    • 1
    • 2
    • 3
    • 4

    2查询月薪最高的员工姓名和月薪

    -- 比较好的几种方法
    select ename,sal 
    from tb_emp
    where sal = (select max(sal) from tb_emp);
    -- 方法2
    select ename,sal 
    from tb_emp as t1
    where (select count(*) from tb_emp as t2 where t2.sal>t1.sal) = 0;
    -- 方法3
    select ename,sal
    from tb_emp as t1
    where sal>=all((select sal from tb_emp));
    -- 方法4
    select ename,sal
    from tb_emp as t1
    where not exists (select 'x' from tb_emp as t2 where t2.sal>t1.sal);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3查询月薪前3名的员工的姓名和月薪

    select ename,sal 
    from tb_emp as t1
    where (select count(*) from tb_emp as t2 where t2.sal>t1.sal)<3
    order by sal desc;
    
    • 1
    • 2
    • 3
    • 4

    4查询员工的姓名和年薪(年薪=(sal+comm)*13)

    select ename,(sal+coalesce(comm,0))*13 
    from tb_emp ;
    
    • 1
    • 2

    5查询部门的编号和人数

    select dno,count(eno) 
    from tb_emp 
    group by dno;
    
    • 1
    • 2
    • 3

    6查询部门人数超过5个人的部门的编号和人数

    -- 方法1
    select dno as 编号,c_c as 人数 
    	from (select dno,count(eno) as c_c from tb_emp group by dno) as newli 
    where c_c>5; 
    -- 比较完善的方法
    select dno as 编号,count(*) as 人数 
    from tb_emp group by dno
    having count(*)>5;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7查询所有部门的名称和人数

    select dname,coalesce(c_dperson,0) 
    from tb_dept as a 
    	left join 
    			(select dno,count(eno) as c_dperson 
                from tb_emp 
                group by dno) as newli1
    	on a.dno = newli1.dno;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8查询月薪超过平均月薪的员工的姓名和月薪

    select ename,sal 
    from tb_emp 
    where sal>(select avg(sal) from tb_emp);
    
    • 1
    • 2
    • 3

    9查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪

    select ename,dno,sal
    from tb_emp natural join 
    		(select dno,avg(sal) as avg_s 
               from tb_emp 
             group by dno) as a
    where sal>avg_s;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    10查询部门中月薪最高的人姓名、月薪和所在部门名称

    select ename,sal,dname
    from tb_emp natural join tb_dept
    natural join (select dno,max(sal) as max_sal 
    			from tb_emp group by dno) as newli
    where sal = max_sal;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    11查询主管的姓名和职位

    select ename,job 
    	from tb_emp 
    where eno in (select mgr from tb_emp);
    
    • 1
    • 2
    • 3

    12查询普通员工的姓名和职位

    select ename,job 
    	from tb_emp 
    where eno not in (select distinct mgr 
    					from tb_emp 
    				  where mgr is not null);
    方法2
    select ename,job 
    	from tb_emp as t1
    where not exists (select distinct mgr 
    					from tb_emp as t2 
                      where t2.mgr = t1.eno);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    13查询主管和普通员工的平均月薪

    select tag
    ,avg(sal)
    from (select sal
    ,case when exists (select 'x' 
    					from tb_emp as t2 
    					where t1.eno = t2.mgr)
    	 then '主管'
         else '普通员工'
         end as tag
    from tb_emp as t1) as tmp
    group by tag;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    14查询月薪排名4~6名的员工排名、姓名和月薪

    select ename,sal 
    	from tb_emp 
    	order by sal desc 
    limit 3 
    offset 3;
    -- 方法2存在相同月薪情况,实际上显示的是行号,因此还需要改进方法
    select * from
    (select @a:=+@a+1 as rn,ename,sal 
    	from tb_emp ,(select @a:=0) as li
    	order by sal desc) as tmp
     where rn between 4 and 6;
      -- 改进的方法3 ,mysql8的方法
     select * from
    	(select rank() over(order by sal desc) as rk1
        -- 两种不同的排名方法
    	-- ,dense_rank() over(order by sal desc) as rk2
    	-- ,row_number() over(order by sal desc) as rk3
    	,ename,sal 
    	from tb_emp) as li
    where rk1 between 4 and 6;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    15查询每个部门月薪排前2名的员工姓名、月薪和部门编号

    select ename,sal,dno from (
    select * from(
    	select ename,sal,dno
    			,rank() over(partition by dno 
    						 order by sal desc) as rk
    	from tb_emp))
    where rk<=2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    建立临时备份表

    -- 从xxxx中备份一个临时表xxx_bak
    create temporary table xxx_bak select * from xxxx;
    
    create temporary table copy_bak select eno,ename,dno from tb_emp;
    
    • 1
    • 2
    • 3
    • 4

    数据控制语言

    创建用户

    create user '名'@'位置' identified by '密码' ;
    
    -- 授权
    grant select on hrs.* to '名'@'位置';
    grant delete,update,insert on hrs.* to '名'@'位置';
    
    -- 权限收回
    revoke delete,update,insert on hrs.* from '名'@'位置';
    
    -- 授予所有权限
    grant all privileges on *.* to '名'@'位置';
    
    -- 拥有跟超级管理员一样的权限
    grant all privileges on *.* to '名'@'位置' with grant option;
    
    -- 收回权限
    revoke all privileges on *.* from '名'@'位置';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    python连接数据库

    执行命令下载包:pip install pymysql cryptography

    建立连接:

    import pymysql
    
    # 创建连接--->connection
    conn = pymysql.connect(host='xxxx', port=3306, user='名字'
                           , password='xxxx', database='hrs', charset='utf8mb4')
    print(conn)
    #出现如下
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    高精度算法【Java】(待更新中~)
    真嘟假嘟?!这么清晰简单的字符函数和字符串函数!!!
    Spring01之ioc
    在vue2中,v-model和.sync的区别
    [计算机毕业设计]关联挖掘的服装推荐系统
    Thread.sleep(0);的作用
    Spring的创建和使用
    C语言学习:4、C语言的运算
    牛血清白蛋白/人血清白蛋白/卵清白蛋白/小鼠血清白蛋白纳米粒改性Angiopep血管肽(2022已更新)
    一个.Net开发的功能强大、易于使用的流媒体服务器和管理系统
  • 原文地址:https://blog.csdn.net/m0_52957800/article/details/127673924