zoukankan      html  css  js  c++  java
  • mysql 中的单表查询

    单表查询:

    create table emp (
    id int primary key auto_increment,
    name varchar(16) 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,salary,office,depart_id) values
    ('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部
    ('egon','male',78,'20150302','teacher',1000000.31,401,1),
    ('kevin','male',81,'20130305','teacher',8300,401,1),
    ('tank','male',73,'20140701','teacher',3500,401,1),
    ('owen','male',28,'20121101','teacher',2100,401,1),
    ('jerry','female',18,'20110211','teacher',9000,401,1),
    ('nick','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)
    ;


    where 约束条件
    # 1.查询id大于等于3小于等于6的数据
    select id name from emp where id >=3 and id <=6;
    select * from emp where id between 3 and 6;
    # 2.查询薪资是20000或者18000或者17000的数据
    select *from emp where salary =2000 or salary =18000or salary =17000;
    select *from emp where salary in(20000,18000,17000);
    # 3.查询员工姓名中包含o字母的员工姓名和薪资 模糊查询
    select name,salary from emp where name like '%o%';
    # 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
    select name,salary from emp where name like '____';
    select name,salary from emp where char_length(name) = 4;
    # 5.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
    select name,post from emp where post_comment = NULL; # 查询为空!
    select name,post from emp where post_comment is NULL;
    select name,post from emp where post_comment is not NULL;

    group by 约束条件
    group by我们可以先从字面上来理解,GROUP表示分组,BY后面写字段名,就表示根据哪个字段进行分组,如果有用Excel比较多的话,GROUP BY比较类似Excel里面的透视表。
    group by必须得配合聚合函数来用,分组之后你可以计数(COUNT),求和(SUM),求平均数(AVG)等。
    常用聚合函数:求个数count 求总和 sum ,求最大值 max 求最小值 min 求平均值 avg

    1.按部门分组
    select * from emp group by post; # 分组后取出的是每个组的第一条数据
    select id,name,sex from emp group by post; # 验证
    """
    设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据,
    不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取
    """
    set global sql_mode="strict_trans_tables,only_full_group_by";
    # 重新链接客户端
    select * from emp group by post; # 报错
    select id,name,sex from emp group by post; # 报错
    select post from emp group by post; # 获取部门信息
    # 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名


    # 2.获取每个部门的最高工资
    # 以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
    # 每个部门的最高工资
    select post,max(salary) from emp group by post;
    # 每个部门的最低工资
    select post,min(salary) from emp group by post;
    # 每个部门的平均工资
    select post,avg(salary) from emp group by post;
    # 每个部门的工资总和
    select post,sum(salary) from emp group by post;
    # 每个部门的人数
    select post,count(id) from emp group by post;


    # 3.查询分组之后的部门名称和每个部门下所有的学生姓名
    # group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
    select post,group_concat(name) from emp group by post;

    select post,group_concat(name,"_SB") from emp group by post;

    select post,group_concat(name,": ",salary) from emp group by post;

    select post,group_concat(salary) from emp group by post;


    # 4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用
    select name as 姓名,salary as 薪资 from emp;
    select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;

    # 补充as语法 即可以给字段起别名也可以给表起
    select emp.id,emp.name from emp as t1; # 报错 因为表名已经被你改成了t1
    select t1.id,t1.name from emp as t1;

    # 查询四则运算
    # 查询每个人的年薪
    select name,salary*12 as annual_salary from emp;
    select name,salary*12 annual_salary from emp; # as可以省略

    having 约束条件
    having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!
    select post,avg(salary) from emp
    where age >= 30
    group by post
    having avg(salary) > 10000;

    #强调:having必须在group by后面使用

    distinct
    # 对有重复的展示数据进行去重操作
    select distinct post from emp;

    order by 排序约束条件
    select * from emp order by salary asc; #默认升序排
    select * from emp order by salary desc; #降序排

    select * from emp order by age desc; #降序排

    #先按照age降序排,在年轻相同的情况下再按照薪资升序排
    select * from emp order by age desc,salary asc;

    # 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
    select post,avg(salary) from emp
    where age > 10
    group by post
    having avg(salary) > 1000
    order by avg(salary)
    ;


    limit 限制约束条件
    # 限制展示条数
    select * from emp limit 3;
    # 查询工资最高的人的详细信息
    select * from emp order by salary desc limit 1;

    # 分页显示
    select * from emp limit 0,5; # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置
    select * from emp limit 5,5;


    正则匹配约束条件
    select * from emp where name regexp '^j.*(n|y)$';
    承蒙关照
  • 相关阅读:
    zoj 3591 Nim 博弈论
    zoj 3529 A Game Between Alice and Bob 博弈论
    zoj 3513 Human or Pig 博弈论
    hdu 3886 Final Kichiku “Lanlanshu” 数位DP
    2013 ACM/ICPC Asia Regional Changsha Online
    2013 ACM/ICPC Asia Regional Changsha Online G Goldbach
    hdu 4753 Fishhead’s Little Game 博弈论+记忆化搜索
    hdu 1404/zoj 2725 Digital Deletions 博弈论
    zoj 2686 Cycle Game 博弈论
    zoj 2290 Game 博弈论
  • 原文地址:https://www.cnblogs.com/guanlei/p/10870434.html
Copyright © 2011-2022 走看看