zoukankan      html  css  js  c++  java
  • mysql数据库

    1.基本查询语句及方法

    2.连表

    3.子查询

    一.基本的查询语句及方法

    首先我们需要明确的一点就是:sql语句的 书写顺序 和 执行顺序
    1.书写顺序:
    
      select id ,name from emp where id < 3 and id < 6;
    2.执行顺序:
      from:确定到底是查那张表
      where:根据后面的条件来筛选数据
      select:拿出筛选出来的数据中的你需要的字段

    当表的字段特别多的时候,结果的排版可能会出现混乱的现象,你可以在查询语句加G来规范查询结果:select * from empG;

    1.基本的查询语句及方法:在这之前需要先创建出1张表来操作

    创建表及添加数据的代码:

     1 create table emp(
     2   id int not null unique auto_increment,
     3   name varchar(20) not null,
     4   sex enum('male','female') not null default 'male', #大部分是男的
     5   age int(3) unsigned not null default 28,
     6   hire_date date not null,
     7   post varchar(50),
     8   post_comment varchar(100),
     9   salary double(15,2),
    10   office int, #一个部门一个屋子
    11   depart_id int
    12 );
    13 
    14 #插入记录
    15 #三个部门:教学,销售,运营
    16 insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
    17 ('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部
    18 ('egon','male',78,'20150302','teacher',1000000.31,401,1),
    19 ('kevin','male',81,'20130305','teacher',8300,401,1),
    20 ('tank','male',73,'20140701','teacher',3500,401,1),
    21 ('owen','male',28,'20121101','teacher',2100,401,1),
    22 ('jerry','female',18,'20110211','teacher',9000,401,1),
    23 ('nick','male',18,'19000301','teacher',30000,401,1),
    24 ('sean','male',48,'20101111','teacher',10000,401,1),
    25 
    26 ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
    27 ('丫丫','female',38,'20101101','sale',2000.35,402,2),
    28 ('丁丁','female',18,'20110312','sale',1000.37,402,2),
    29 ('星星','female',18,'20160513','sale',3000.29,402,2),
    30 ('格格','female',28,'20170127','sale',4000.33,402,2),
    31 
    32 ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
    33 ('程咬金','male',18,'19970312','operation',20000,403,3),
    34 ('程咬银','female',18,'20130311','operation',19000,403,3),
    35 ('程咬铜','male',18,'20150411','operation',18000,403,3),
    36 ('程咬铁','female',18,'20140512','operation',17000,403,3)
    37 ;
    View Code

    2.先说一些最基础的查询方法:

     1 # 1.查询id大于等于3小于等于6的数据
     2 select * from emp where id >=3 and id <= 6;
     3 select * from emp where id between 3 and 6;
     4 上述语句完全等价
     5 
     6 # 2.查询薪资是20000或者18000或者17000的数据
     7 select id,name from emp where salary = 20000 or salary = 18000 or salary = 17000;
     8 select id,name from emp where salary in (20000,18000,17000);
     9 
    10 # 3.查询员工姓名中包含o字母的员工姓名和薪资
    11 模糊匹配  like
    12         %:匹配多个任意字符
    13         _:匹配一个任意字符
    14 select name,salary from emp where name like '%o%';
    15 
    16 # 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
    17 select name,salary from emp where name like '____';
    18 
    19 # 5.查询id小于3或者大于6的数据
    20 select * from emp where id < 3 or id > 6;
    21 select * from emp where id not between 3 and 6;
    22 
    23 # 6.查询薪资不在20000,18000,17000范围的数据
    24 select id,name from emp where salary not in (20000,18000,17000);
    25 
    26 # 7.查询岗位描述为空的员工名与岗位名   针对null判断的时候只能用is 不能用=
    27 select name,post from emp where post_comment = Null;
    28 select name,post from emp where post_comment is Null;
    29     
    30     MySQL对大小写不敏感
    View Code

    3.接下来介绍几个方法:

    group by :分组

    聚合函数:max 最大 ,min  最小  , avg 平均  , sum 求和 ,count 计数

    聚合函数会自动将每一个分组内的单个数据做想要的计算,无需你考虑

    这里有几点需要注意的:

    1.分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息
    2.MySQL中分组之后 只能拿到分组的字段信息 无法直接获取其他字段信息
    但是你可以通过其他方法(聚合函数)简介的获取
    
    3.数据分组应用场景:每个部门的平均薪资,男女比例等
    
    # 1.按部门分组
    select * from emp group by post;
    这样写是错的,那么为什么mysql没有给他报错了,因为你没有设置严格模式:
    
    如何来设置严格模式:
        show variables like '%mode%';
        set session  当前窗口有效
        set global  全局有效 
        set global sql_mode="strict_trans_tables,only_full_group_by";
    注意:设置完严格模式后一定要重启客户端
    
    总结:
        设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据,
    不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取

    注意:聚合函数 max min sum count avg只能在分组之后使用
    如果一张表没有写group by默认所有的数据就是一组
    """
    书写顺序
    select
    from
    where
    group by

    
    

    执行顺序
    from
    where
    group by
    select

    一些注意点说完了,接下来就可以介绍操作了:

     1 # 1.按部门分组
     2 select * from emp group by post;  # 报错
     3 select id,name,sex from emp group by post;  # 报错
     4 select post from emp where group by post; # 获取部门信息  正确的
     5 
     6 # 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名
     7 
     8 # 2.获取每个部门的最高工资  
     9 # 以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
    10 # 每个部门的最高工资
    11 select post,max(salary) from emp group by post;
    12 # 每个部门的最低工资
    13 select post,min(salary) from emp group by post;
    14 # 每个部门的平均工资
    15 select post,avg(salary) from emp group by post;
    16 # 每个部门的工资总和
    17 select post,sum(salary) from emp group by post;
    18 # 每个部门的人数
    19 select post,count(id) from emp group by post;
    View Code

    4.group_concat

    注意点:group_concat()能够拿到分组后每一个数据指定字段(可以是多个)对应的值

    
        小技巧:
            concat就是用来帮你拼接数据
            concat 不分组情况下使用
            group_concat  分组之后使用    
    
    # 刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表也没有限制条件,再确定是否需要分类,最后再确定需要什么字段对应的信息
    注意:

    写sql语句的时候 一定不要一口气写完
    前期先按照步骤一步步写
    写一步查询看一下结果然后基于当前结果再往后写

    View Code

    练习题:代码

     1 1. 查询岗位名以及岗位包含的所有员工名字
     2 2. 查询岗位名以及各岗位内包含的员工个数
     3 3. 查询公司内男员工和女员工的个数
     4 4. 查询岗位名以及各岗位的平均薪资
     5 5. 查询岗位名以及各岗位的最高薪资
     6 6. 查询岗位名以及各岗位的最低薪资
     7 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
     8 """
     9 参考答案:
    10 select post,group_concat(name) from emp group by post;
    11 select post,count(id) from emp group by post;
    12 select sex,count(id) from employee group by sex;
    13 select post,avg(salary) from emp group by post;
    14 select post,max(salary) from employee group by post;
    15 select post,min(salary) from employee group by post;
    16 select sex,avg(salary) from employee group by sex;
    17 """
    18 
    19 # 关键字where group by同时出现的情况下,group by必须在where之后
    20 # where先对整张表进行一次筛选,如何group by再对筛选过后的表进行分组
    21 # 如何验证where是在group by之前执行而不是之后 利用聚合函数 因为聚合函数只能在分组之后才能使用
    22 select id,name,age from emp where max(salary) > 3000;  # 报错!
    23 
    24 select max(salary) from emp;  
    25 # 正常运行,不分组意味着每一个人都是一组,等运行到max(salary)的时候已经经过where,group by操作了,只不过我们都没有写这些条件
    26 
    27 # 语法顺序
    28 select
    29 from
    30 where
    31 group by
    32 
    33 # 再识执行顺序
    34 from
    35 where 
    36 group by
    37 select
    38 
    39 
    40 8、统计各部门年龄在30岁以上的员工平均工资
    41 select post,avg(salary) from emp where age > 30 group by post; 
    42 # 对where过滤出来的虚拟表进行一个分组
    43 
    44 # 还不明白可以分步执行查看结构
    45 select * from emp where age>30;
    46 # 基于上面的虚拟表进行分组
    47 select * from emp where age>=30 group by post;
    View Code

    5.having(二次筛选)

     1 having
     2 1.跟where是一模一样的 也是用来筛选数据,但是having是跟在group by之后的,然而where是对整体数据做一个初步的筛选。
     3 
     4 2.然而having是对分组之后的数据再进行一次针对性的筛选
     5 
     6 # 强调:having必须在group by后面使用,不能单独使用
     7 
     8 having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!
     9 
    10 案例:
    11 
    12 1、统计各部门年龄在30岁以上的员工平均工资,
    13 并且保留平均工资大于10000的部门
    14 select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;
    View Code

    6.distinct(去重)

     1 去重必须数据是一模一样的才能去重
     2 只要有一个不一样 都不能算是的重复的数据
     3 select distinct id,age from emp;
     4     执行顺序
     5     from 
     6     where
     7     group by
     8     having
     9     select
    10     distinct
    11     
    View Code

    7.order by (排序)

     1 order by 排序  默认的
     2 默认是升序 asc
     3 也可以变成降序  desc
     4 
     5 select * from emp order by salary; # 默认升序
     6 select * from emp order by salary asc; # 升序
     7 select * from emp order by salary desc; #降序
     8 select * from emp order by age,salary;   # 先按照age做升序 age相同的情况下再按照salary做升序
     9 select * from emp order by age asc,salary desc;   # 先按照age做升序 age相同的情况下再按照salary做升序
    10     
    11     
    12 # 统计各部门年龄在10岁以上的员工平均工资,
    13 # 并且保留平均工资大于1000的部门,然后对平均工资进行排序
    14 select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);
    View Code

    8.limit(限制展示条数)

     1 limit  限制展示数据的条数
     2 select * from emp limit 5;  # 只展示数据的五条
     3 select * from emp limit 5,5;  
     4     
     5     
     6 当limit只有一个参数的时候  表示的是只展示几条
     7 当limit有两个参数的时候   第一个参数表示的起始位置 第二个参数表示从起始位置开始往后展示的条数
     8     
     9 # 查询工资最高的人的详细信息
    10 # 先按照薪资排序
    11 # 再用limit限制 只取一条
    12     
    13 select * from emp order by salary desc limit 1;
    View Code

    9.正则

     1 # 在编程中 只要看到reg开头的 基本上都是跟正则相关
     2         
     3 正则
     4 select * from emp where name regexp '^j.*(n|y)$';
     5         
     6 jason
     7 jssdsdsay
     8 jy
     9 jnn
    10     
    View Code

    二.多表查询

    先创建表:

     1 create table dep(
     2 id int,
     3 name varchar(20) 
     4 );
     5 
     6 create table emp(
     7 id int primary key auto_increment,
     8 name varchar(20),
     9 sex enum('male','female') not null default 'male',
    10 age int,
    11 dep_id int
    12 );
    13 
    14 #插入数据
    15 insert into dep values
    16 (200,'技术'),
    17 (201,'人力资源'),
    18 (202,'销售'),
    19 (203,'运营');
    20 
    21 insert into emp(name,sex,age,dep_id) values
    22 ('jason','male',18,200),
    23 ('egon','female',48,201),
    24 ('kevin','male',38,201),
    25 ('nick','female',28,202),
    26 ('owen','male',18,200),
    27 ('jerry','female',18,204)
    28 ;
    View Code

    那么多表查询一共分为2大类:
    1.连表查询

    2.子查询

    首先来说一说多表查询吧,如果你要查询查询部门为技术部的员工及部门信息,那么你需要将俩张表连起来。
    
    那么有这么4种连表的查询:
    
    内连接(inner join)
            
    左连接(left join)
        
    右连接(right join)
        
    全连接(union)  # 只要将左连接和右连接的sql语句 加一个union就变成全连接

    代码:

     1 select * from emp,dep;  # 左表一条记录与右表所有记录都对应一遍>>>笛卡尔积
     2 
     3 # 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据
     4 
     5 # 查询员工及所在部门的信息
     6 select * from emp,dep where emp.dep_id = dep.id;
     7 # 查询部门为技术部的员工及部门信息
     8 select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';
     9 
    10 以上的方法是不合理的:
    11 
    12 # 将两张表关联到一起的操作,有专门对应的方法
    13 # 1、内连接:只取两张表有对应关系的记录
    14 select * from emp inner join dep on emp.dep_id = dep.id;
    15 select * from emp inner join dep on emp.dep_id = dep.id
    16                             where dep.name = "技术";
    17 
    18 # 2、左连接: 在内连接的基础上保留左表没有对应关系的记录
    19 select * from emp left join dep on emp.dep_id = dep.id;
    20 
    21 # 3、右连接: 在内连接的基础上保留右表没有对应关系的记录
    22 select * from emp right join dep on emp.dep_id = dep.id;
    23 
    24 # 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
    25 select * from emp left join dep on emp.dep_id = dep.id
    26 union
    27 select * from emp right join dep on emp.dep_id = dep.id;
    View Code

    三.子查询

    就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用
    1.查询部门是技术或者人力资源的员工信息

    # 可以给表起别名
    # 可以给查询出来的虚拟表起别名
    # 可以给字段起别名

     1 # 就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用
     2 # 1.查询部门是技术或者人力资源的员工信息
     3 """
     4 先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息
     5 """
     6 select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");
     7 
     8 # 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询
     9 select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
    10 inner join
    11 (select post,max(hire_date) as max_date from emp group by post) as t2
    12 on t1.post = t2.post
    13 where t1.hire_date = t2.max_date
    14 ;
    15 
    16 """
    17 记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询
    18 """
    19 
    20 select * from emp inner join dep on emp.dep_id = dep.id;
    View Code
  • 相关阅读:
    分享我设计的iOS项目目录结构
    swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)
    swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象
    获取UIColor中的RGB值(本人亲测多个获取RGB值的方法,这个最有效)
    swift语言开发的一个游戏------熊猫跑酷(KongfuPanda)
    ios上传应用后,审核流程完成前(reveiw)修改了程序内容,如何上传替换
    上架app 到app store 的出现: “The IPA is invalid. It does not inlude a Payload directory.”错误处理
    ios 8+ (xcode 6.0 +)应用程序Ad Hoc 发布前多设备测试流程详解
    Swift 实现iOS Animation动画教程
    新浪微博项目---首页技术点三.上拉刷新,下拉加载的实现(使用ios自带的小菊花实现)
  • 原文地址:https://www.cnblogs.com/zahngyu/p/11388920.html
Copyright © 2011-2022 走看看