码迷,mamicode.com
首页 > 数据库 > 详细

数据库之单表查询

时间:2018-05-12 22:29:15      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:min   asc   销售部   type   concat   auto   语法   sql_mode   一模一样   

select * from emp where name regexp ^jin.*(n|g)$;

 

技术分享图片

找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录
3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
4.将分组的结果进行having过滤
5.执行select
6.去重
7.将结果按条件排序:order by
8.限制结果的显示条数
详细:http://www.cnblogs.com/linhaifeng/articles/7372774.html

技术分享图片

 

 

准备表和记录
company.employee
    员工id      id                  int
    姓名        emp_name            varchar
    性别        sex                 enum
    年龄        age                 int
    入职日期     hire_date           date
    岗位        post                varchar
    职位描述     post_comment        varchar
    薪水        salary              double
    办公室       office              int
    部门编号     depart_id           int



#创建表
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
);


#查看表结构
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field        | Type                  | Null | Key | Default | Extra          |
+--------------+-----------------------+------+-----+---------+----------------+
| id           | int(11)               | NO   | PRI | NULL    | auto_increment |
| name         | varchar(20)           | NO   |     | NULL    |                |
| sex          | enum(male,female) | NO   |     | male    |                |
| age          | int(3) unsigned       | NO   |     | 28      |                |
| hire_date    | date                  | NO   |     | NULL    |                |
| post         | varchar(50)           | YES  |     | NULL    |                |
| post_comment | varchar(100)          | YES  |     | NULL    |                |
| salary       | double(15,2)          | YES  |     | NULL    |                |
| office       | int(11)               | YES  |     | NULL    |                |
| depart_id    | int(11)               | YES  |     | NULL    |                |
+--------------+-----------------------+------+-----+---------+----------------+

#插入记录
#三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
(egon,male,18,20170301,老男孩驻沙河办事处外交大使,7300.33,401,1), #以下是教学部
(alex,male,78,20150302,teacher,1000000.31,401,1),
(wupeiqi,male,81,20130305,teacher,8300,401,1),
(yuanhao,male,73,20140701,teacher,3500,401,1),
(liwenzhou,male,28,20121101,teacher,2100,401,1),
(jingliyang,female,18,20110211,teacher,9000,401,1),
(jinxin,male,18,19000301,teacher,30000,401,1),
(成龙,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

技术分享图片

技术分享图片

小练习:
    select id,name from db39.emp where id >= 3 and id <= 6;
    select *  from db39.emp where id between 3 and 6;


    select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
    select * from emp where salary in (20000,18000,17000);


    要求:查询员工姓名中包含i字母的员工姓名与其薪资
    select name,salary from db39.emp where name like %i%

    要求:查询员工姓名是由四个字符组成的的员工姓名与其薪资
    select name,salary from db39.emp where name like ____;
    select name,salary from db39.emp where char_length(name) = 4;



    select *  from db39.emp where id not between 3 and 6;
    select * from emp where salary not in (20000,18000,17000);

    要求:查询岗位描述为空的员工名与岗位名
    select name,post from db39.emp where post_comment is NULL;
    select name,post from db39.emp where post_comment is not NULL;

技术分享图片

   一 什么是分组?为什么要分组?
    #1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的
    #2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等
    #3、为何要分组呢?
        取每个部门的最高工资
        取每个部门的员工数
        取男人数和女人数

    小窍门:‘每’这个字后面的字段,就是我们分组的依据
    #4、大前提:
        可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数

   二,only_full_group_by  (聚合函数)
   三,group by
    #设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据默认
    mysql> set global sql_mode="strict_trans_tables,only_full_group_by";

    #每个部门的最高工资
    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;

技术分享图片

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;

技术分享图片

技术分享图片

技术分享图片

  select name as 姓名,salary as 薪资 from emp;
    mysql> select emp.id,emp.name from emp as t1; # 报错
    mysql> select t1.id,t1.name from emp as t1;

技术分享图片

技术分享图片

技术分享图片

 

  having的语法格式与where一模一样,只不过having是在分组之后进行的进一步过滤
    即where不能用聚合函数,而having是可以用聚合函数,这也是他们俩最大的区别

    1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
    select post,avg(salary) from emp
            where age >= 30
            group by post
            having avg(salary) > 10000;

    #强调:having必须在group by后面使用
    select * from emp
            having avg(salary) > 10000;

技术分享图片

技术分享图片

技术分享图片

技术分享图片

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

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

select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪资升序排



# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,
然后对平均工资进行排序

select post,avg(salary) from emp
    where age > 10
    group by post
    having avg(salary) > 1000
    order by avg(salary)
    ;

技术分享图片

 

 

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 ^jin.*(n|g)$;

 

数据库之单表查询

标签:min   asc   销售部   type   concat   auto   语法   sql_mode   一模一样   

原文地址:https://www.cnblogs.com/maojiang/p/9029965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!