• 表查询


    表查询

    创建一个表插入数据
    create table emp(
     id int not null unique auto_increment,
     name varchar(20) not null,
     sex enum('male','female') not null default 'male', #大部分是男的
     age int(3) unsigned not null default 28,
     hire_date date not null,
     post varchar(50),
     post_comment varchar(100),
     salary double(15,2),
     office int, # 一个部门一个屋子
     depart_id int
    );
    insert into emp(name,sex,age,hire_date,post,salart,office,depary_id) values
    ('tank','male',17,'20170301','张江第一帅形象代言部门',7300.33,401,1), # 以下是教学部
    ('egon','male',78,'20150302','teacher',1000000.31,401,1),
    ('kevin','male',81,'20130305','teacher',8300,401,1),
    ('jason','male',73,'20140701','teacher',3500,401,1),
    ('owen','male',28,'20121101','teacher',2100,401,1),
    ('jerry','female',18,'20110211','teacher',9000,401,1),
    ('大饼','male',18,'19000301','teacher',30000,401,1),
    ('sean','male',48,'20101111','teacher',10000,401,1),

    ('歪歪','female',48,'20150311','sale',3000.13,402,2),# 以下是销售部门
    ('丫丫','female',38,'20101101','sale',2000.35,402,2),
    ('丁丁','female',18,'20110312','sale',1000.37,402,2),
    ('星星','female',18,'20160513','sale',3000.29,402,2),
    ('格格','female',28,'20170127','sale',4000.33,402,2),

    ('张野','male',28,'20160311','operation',10000.13,403,3), # 以下是运营部门
    ('程咬金','male',18,'19970312','operation',20000,403,3),
    ('程咬银','female',18,'20130311','operation',19000,403,3),
    ('程咬铜','male',18,'20150411','operation',18000,403,3),
    ('程咬铁','female',18,'20140512','operation',17000,403,3);

    # PS:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk

    语法书写与执行顺序

    在写SQL命令时注意:
    1.书写顺序: select () from () where
       2.执行顺序:from () where () select ()
    查询数据量较大时,可在表后面加G,修改显示格式
    select * from empG

    where 约束条件

    #查询ID在3-6之间的数据
    '''
    and: 与
    between:两者之间
    '''
    select * from emp where id >3 and id <6;
    select * from emp where id between 3 and 6;
    #查询薪资是20000或18000或17000的数据
    '''
    or: 或者
    in : 在什么里面
    '''
    select * from emp where salary=20000 or salary=18000 or salary= 17000;
    select * from emp where salary in (20000,18000,17000);
    #查询员工名中包含o的员工姓名与工资
    '''
    like:模糊匹配
    %:匹配0个或多个任意字符
    ——:匹配一个任意符
    char_length(字段):获取字段长度
    '''
    select name,salary from emp where name like "%o%";
    #查询员工姓名由四个字组成的员工的姓名与工资
    select name,salary from emp where name like"____";
    select name,salary from emp where char_length(name)=4;
    #查询岗位描述为空的员工名与岗位名
    #注意针对null不能用=只能欧诺个is
    select name,post from emp where post_comment is null;

    group by 分组

    #group by :分组
    '''
    聚合函数:max,min,sum,avg,count
    as:起别名,给获取出来的数据字段名,设置别名
    group_concat():不仅可以获取分组后的某个字段,还可以对字符串进行拼接
    '''
    #设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取分组的依据,不应该在去取组里的单个元素的值,那样分组就没有多大意义了,因为不分组就是多对单个元素信息的随意取值
    #获取每个部门的最高工资
    select post,max(salary) from emp group by post;
    select post as '部门', max(salary) as '薪资' from emp group by post;
    #查询每个部门最低工资:
    select post,min(salary) from emp group by post;
    #查询每个部门的工资和
    select post,sum(salary) from emp group by post;
    #查询每个部门的平均工资
    select post,avg(salary) from emp group by post;
    #查看每个部门几个人
    select post,count(任意值) from emp group by post;
    #查看各个部门下的员工姓名
    select post,group_concat(name) from emp group by post;
    #在员工姓名前加NB
    select post,group_concat('NB',name) from emp group by post;
    #拼接员工姓名与工资
    select post,group_concat(name,':',salary)from emp group by post;
    #concat拼接字符可以达到更好的效果,as 可以给表起别名
    select concat('Name: ', name) as '名字', concat('Sal: ', salary) as '薪资' from emp;

    having: 过滤

        1.having与where语法一样,只是having需要在group by 后使用;
    2.where不能使用聚合函数,但having可以
    1.统计各部门年龄在30岁以上的员工品骏工资,并保留平均工资大于10000的部门
    select post,avg(salary) from emp where age>30 group by post having avg(salary)>10000;

    distinct:去重

    distinct:去重
    #注意查询的数据值必须是重复的才有效,只要有一个字段值是不重复的就没有效果。
    select distinct post from emp;

    order by:排序

    order by:排序,获取select获取的数据进行排序
    1.根据薪资进行升序
    select * from emp order by salary; #默认升序
    2.根据年龄进行降序
    select * from emp order by age desc;#制定降序
    3.按照age升序,再按照salary升序
    select * from emp order by age asc,salary desc;
    4.统计各部门(分组)年龄在10岁以上的员工平均工资,并保留平均工资大于1000的部门,然后对平均工资进行排序
    select post ,avg(salary) from emp where age>10 group by post having avg(salary)>1000 order by avg(salary);

    limit :限制结果返回值

    #limnit:限制结果返回值
    #应用场景:类似于博客园首页的数据展示,每一页有固定的数量
    1.从第一条开始获取四条记录
    select * from emp limit 4;
    select * from emp limit 0,4;#前一个参数从第几个截取
    2.查询工资最高的人的信息
    select * from emp order by  salary desc limit 1;

    正则:

    #在编程里凡是reg开头的基本上都与正则有关
    1.查看程子开头的人的信息
    select * from emp where name regexp '^程.*';
    

    多表查询

    关联查询

    子查询

    创建表与插入数据

    create table dep2(id int,name varchar(20) );
    create table emp2(id int primary key auto_increment,name varchar(20),sex enum('male','female') not null default 'male',age int,dep_id int);
    insert into dep2 values(200,'技术'),(201,'人力资源'),(202,'销售'),(203,'运营');
    insert into emp2(name,sex,age,dep_id) values
    ('tank','male',17,200),('egon','female',48,201),('kevin','male',38,201),('jason','female',28,202),('owen','male',18,200),('sean','female',18,204);
    # 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据
    1.查询员工所在的部门
    select * from emp2,dep2 where emp2.dep_id = dep2.id;
    2.查看部门为技术部的员工及部门信息
    select * from emp2,dep2 where emp2.dep_id = dep2.id and dep2.name = '技术';
    1.内连接:只取两张表有对应的方法
    inner join
    select * from emp2 inner join dep2 on emp2.dep_id = dep2.id;
    2.左连接:在内连接的基础上保留左表没有对应关系的记录:
    left join
    select * from emp2 left join dep2 on emp2.dep_id = dep2.id;
    3.右连接:在内连接的基础上保留右表没有对应关系的记录
    right join
    select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
    4.全连接:在内连接的基础上保留左右表没有对应关系的记录
    select * from emp2 left join dep2 on emp2.dep_id = dep2.id union select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
    
    

    子查询

    #子查询就是讲一个查询语句的结果当做另一个查询语句的条件使用
    1.查询部门是技术部或人力资源部的员工信息
    select * from emp2 where dep_id in (select id from dep2 where name='技术' or name='人力资源');
    2.查询每个部门最新入职的员工
     select t1.name,t1.id,t1.hire_date,t1.post,t2.* from emp as t1 inner join (select post,max(hire_date)as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date;
    

    exec内置函数:是一个python内置的函数,可以将字符串代码添加待名称空间中

    code = '''
    global x
    x = 100
    
    y = 20
    def func():
        pass
        
    def __init__():
        pass
    '''
    自定义一个全局名称空间
    global_dic = {'x',10000}
    自定义一个局部名称空间
    local——dic = {}
    exec(code,global_dic,local_dic) #跟一个自己的代码字符串+自定义全局名称空间+自定义局部名称空间
    
    
  • 相关阅读:
    关于Spring Test 小结
    排他思想---->tab选项卡
    对金额的格式化
    js 对多个id 的封装方法
    form表单数据封装成json格式并提交给服务器
    js技巧专题篇: 页面跳转
    对象流
    线程
    异常处理、常见异常说明
    数据库(概念、语法、DBMS、SQL语言:创建数据库、表格,添加、修改、删除数据记录)
  • 原文地址:https://www.cnblogs.com/cyfdtz/p/12108009.html
Copyright © 2020-2023  润新知