• Mysql 数据库(五)


     1 插入数据:

       create table 新表名(字段 数据类型[约束条间]。。。) select 字段。。。 from 旧表名

       create table 新表名(字段 数据类型[约束条件]。。。) select 字段 as 别名 from 旧表名 [where 条件];

       as 起一个别名,起别名时,默认有as 所以可以不用加as 就可以起别名。

    mysql> create table t1(id int primary key auto_increment,
        ->                  name char(10) not null,
        ->                  age int not null);
    Query OK, 0 rows affected (0.38 sec)
     
    mysql> insert into t1(name,age)values('fang',18),('haiyan',17),('dong',21);
    Query OK, 3 rows affected (0.06 sec)
    Records: 3  Duplicates: 0  Warnings: 0
     
    mysql> desc t1;
    +-------+----------+------+-----+---------+----------------+
    | Field | Type     | Null | Key | Default | Extra          |
    +-------+----------+------+-----+---------+----------------+
    | id    | int(11)  | NO   | PRI | NULL    | auto_increment |
    | name  | char(10) | NO   |     | NULL    |                |
    | age   | int(11)  | NO   |     | NULL    |                |
    +-------+----------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
     
    mysql> select * from t1;
    +----+--------+-----+
    | id | name   | age |
    +----+--------+-----+
    |  1 | fang   |  18 |
    |  2 | haiyan |  17 |
    |  3 | dong   |  21 |
    +----+--------+-----+
    3 rows in set (0.00 sec)
     
    mysql> create table t2(name char(10) not null,
        ->                  age int not null) select name,age from t1;
    Query OK, 3 rows affected (0.32 sec)
    Records: 3  Duplicates: 0  Warnings: 0
     
    mysql> desc t2;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | name  | char(10) | NO   |     | NULL    |       |
    | age   | int(11)  | NO   |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
     
    mysql> select * from t2;
    +--------+-----+
    | name   | age |
    +--------+-----+
    | fang   |  18 |
    | haiyan |  17 |
    | dong   |  21 |
    +--------+-----+
    3 rows in set (0.00 sec)
     
    mysql> create table t3(x char(10) not null,
        ->          a int not null) select name as x,age a from t1;
    Query OK, 3 rows affected (0.31 sec)
    Records: 3  Duplicates: 0  Warnings: 0
     
    mysql> desc t3;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | x     | char(10) | NO   |     | NULL    |       |
    | a     | int(11)  | NO   |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
     
    mysql> select * from t3;
    +--------+----+
    | x      | a  |
    +--------+----+
    | fang   | 18 |
    | haiyan | 17 |
    | dong   | 21 |
    +--------+----+
    3 rows in set (0.00 sec)
    

     2 修改数据:

       update 表名 set 字段=记录 where 条件;

    mysql> create table t1(id int primary key auto_increment,
        ->                  name char(10) not null,
        ->                  age int not null);
    Query OK, 0 rows affected (0.38 sec)
     
    mysql> insert into t1(name,age)values('fang',18),('haiyan',17),('dong',21);
    Query OK, 3 rows affected (0.06 sec)
    Records: 3  Duplicates: 0  Warnings: 0
     
    mysql> desc t1;
    +-------+----------+------+-----+---------+----------------+
    | Field | Type     | Null | Key | Default | Extra          |
    +-------+----------+------+-----+---------+----------------+
    | id    | int(11)  | NO   | PRI | NULL    | auto_increment |
    | name  | char(10) | NO   |     | NULL    |                |
    | age   | int(11)  | NO   |     | NULL    |                |
    +-------+----------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
     
    mysql> select * from t1;
    +----+--------+-----+
    | id | name   | age |
    +----+--------+-----+
    |  1 | fang   |  18 |
    |  2 | haiyan |  17 |
    |  3 | dong   |  21 |
    +----+--------+-----+
    3 rows in set (0.00 sec)
     
    mysql> update t1 set name='jie' where id=3 and age=21;
    Query OK, 1 row affected (0.08 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
     
    mysql> select * from t1;
    +----+--------+-----+
    | id | name   | age |
    +----+--------+-----+
    |  1 | fang   |  18 |
    |  2 | haiyan |  17 |
    |  3 | jie    |  21 |
    +----+--------+-----+
    3 rows in set (0.00 sec)

    3

      concat:字符串的拼接,可以拼成任意的格式

      concat_ws:第一个参数指定的是分割符,后面加上自己要看的字段

      G:将乱了的记录重新按行显示。

    二 常用的select查看

     select 的基本格式:

      select distinot:去重 字段名。。。 from 表名

        where 约束条件        :取出来的记录默认为是一个组,在这里就可以使用聚合函数,约束条件就是默认某些字段的约束。

        group by 字段名          :后面加上某个字段,就会按照那个字段分组,分组过后可以直接查看分组的字段,要想查看其他的字段,必须要借助聚合函数,分组是为了一类一类的处理数据。而分组的字段是依据字段的约束条件不唯一。

        having 过滤语句         :只能跟在分组的后面,处理一些分组过后的约束条件。

        order by 字段 排序       :排序。后面加上需要按照排序的字段,后面可以同时加上多个排序条件,如果前面的排序有重复的,才会执行后面的,如果没有重复的,后面的就不会执行。他是在取出了相应字段的记录后才开始执行的。

        limit   限制条件              :规定查看的范围,在最后才开始运行。

    准备表:

    mysql> use day44;
    Database changed
    mysql> create table employee(
        -> 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
        -> );
    Query OK, 0 rows affected (0.28 sec)
     
    mysql>
    mysql>
    mysql> #查看表结构
    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    |                |
    +--------------+-----------------------+------+-----+---------+----------------+
    10 rows in set (0.01 sec)
     
    mysql>
    mysql> #插入记录
    mysql> #三个部门:教学,销售,运营
    mysql> insert into employee(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)
        -> ;
    Query OK, 18 rows affected (0.09 sec)
    Records: 18  Duplicates: 0  Warnings: 0
    

     集合函数:min:最小值   max:最大值    avg:计算平均值    sum:计算总和     count:计算个数

      where 条件约束:后面可以加上比较符号(>  <  =  !=   <>  <=  >=)   逻辑运算符(and    or    not)    

              in:什么或什么或什么          between:在什么和什么之间    like:像什么 后面出入两个符号   % :任意多个字符  _ :表示任意一个字符  is:什么是什么

    mysql> select * from employee where age>50;
    +----+---------+------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name    | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+---------+------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex    | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    |  3 | wupeiqi | male |  81 | 2013-03-05 | teacher | NULL         |    8300.00 |    401 |         1 |
    |  4 | yuanhao | male |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
    +----+---------+------+-----+------------+---------+--------------+------------+--------+-----------+
    3 rows in set (0.00 sec)
     
    mysql> select * from employee where sex='male' and salary>30000;
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    1 row in set (0.00 sec)
     
    mysql> select * from employee where post='teacher' or salary>30000;
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex       | male   |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    |  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher | NULL         |    8300.00 |    401 |         1 |
    |  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
    |  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher | NULL         |    2100.00 |    401 |         1 |
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |    9000.00 |    401 |         1 |
    |  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         |   30000.00 |    401 |         1 |
    |  8 | 成龙       | male   |  48 | 2010-11-11 | teacher | NULL         |   10000.00 |    401 |         1 |
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    7 rows in set (0.00 sec)
     
    mysql> select * from employee where age between 20 and 30;
    +----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    | id | name      | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
    +----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    |  5 | liwenzhou | male   |  28 | 2012-11-01 | teacher   | NULL         |  2100.00 |    401 |         1 |
    | 13 | 格格      | female |  28 | 2017-01-27 | sale      | NULL         |  4000.33 |    402 |         2 |
    | 14 | 张野      | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
    +----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    3 rows in set (0.01 sec)
     
    mysql> select * from employee where id in(3,6,16) or name like '程%';
    +----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
    +----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    |  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher   | NULL         |  8300.00 |    401 |         1 |
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher   | NULL         |  9000.00 |    401 |         1 |
    | 15 | 程咬金     | male   |  18 | 1997-03-12 | operation | NULL         | 20000.00 |    403 |         3 |
    | 16 | 程咬银     | female |  18 | 2013-03-11 | operation | NULL         | 19000.00 |    403 |         3 |
    | 17 | 程咬铜     | male   |  18 | 2015-04-11 | operation | NULL         | 18000.00 |    403 |         3 |
    | 18 | 程咬铁     | female |  18 | 2014-05-12 | operation | NULL         | 17000.00 |    403 |         3 |
    +----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    6 rows in set (0.44 sec)
    

    group by 分组:后面加上一个字段名  

    mysql> select post,count(id),group_concat(name) from employee group by post;
    +----------------------------+-----------+-------------------------------------------------------+
    | post                       | count(id) | group_concat(name)                                    |
    +----------------------------+-----------+-------------------------------------------------------+
    | operation                  |         5 | 张野,程咬金,程咬银,程咬铜,程咬铁                      |
    | sale                       |         5 | 歪歪,丫丫,丁丁,星星,格格                              |
    | teacher                    |         7 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 |
    | 老男孩驻沙河办事处外交大使 |         1 | egon                                                  |
    +----------------------------+-----------+-------------------------------------------------------+
    4 rows in set (0.00 sec)
     
    mysql> select post,min(salary) from employee group by post;
    +----------------------------+-------------+
    | post                       | min(salary) |
    +----------------------------+-------------+
    | operation                  |    10000.13 |
    | sale                       |     1000.37 |
    | teacher                    |     2100.00 |
    | 老男孩驻沙河办事处外交大使 |     7300.33 |
    +----------------------------+-------------+
    4 rows in set (0.00 sec)
     
    mysql> select post,max(salary) from employee group by post;
    +----------------------------+-------------+
    | post                       | max(salary) |
    +----------------------------+-------------+
    | operation                  |    20000.00 |
    | sale                       |     4000.33 |
    | teacher                    |  1000000.31 |
    | 老男孩驻沙河办事处外交大使 |     7300.33 |
    +----------------------------+-------------+
    4 rows in set (0.00 sec)
     
    mysql> select post,sum(salary) from employee group by post;
    +----------------------------+-------------+
    | post                       | sum(salary) |
    +----------------------------+-------------+
    | operation                  |    84000.13 |
    | sale                       |    13001.47 |
    | teacher                    |  1062900.31 |
    | 老男孩驻沙河办事处外交大使 |     7300.33 |
    +----------------------------+-------------+
    4 rows in set (0.00 sec)
     
    mysql> select post,avg(salary) from employee group by post;
    +----------------------------+---------------+
    | post                       | avg(salary)   |
    +----------------------------+---------------+
    | operation                  |  16800.026000 |
    | sale                       |   2600.294000 |
    | teacher                    | 151842.901429 |
    | 老男孩驻沙河办事处外交大使 |   7300.330000 |
    +----------------------------+---------------+
    4 rows in set (0.00 sec)
    

    having  过滤条件:后面加的内容和where一样的,不过需要借助于聚合函数过滤记录  

    mysql> select post from employee group by post having count(id)>5;
    +---------+
    | post    |
    +---------+
    | teacher |
    +---------+
    1 row in set (0.00 sec)
     
    mysql> select post from employee group by post having count(id)>5 or avg(salary) >10000;
    +-----------+
    | post      |
    +-----------+
    | operation |
    | teacher   |
    +-----------+
    2 rows in set (0.00 sec)
    

     order by :后面跟上:asc:升序,从小到大排序    ;     desc:降序,从大到小排序。order by:默认是从小到达排序的。 

    mysql> select * from employee order by age asc,id desc;
    +----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post                       | post_comment | salary     | office | depart_id |
    +----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    | 18 | 程咬铁     | female |  18 | 2014-05-12 | operation                  | NULL         |   17000.00 |    403 |         3 |
    | 17 | 程咬铜     | male   |  18 | 2015-04-11 | operation                  | NULL         |   18000.00 |    403 |         3 |
    | 16 | 程咬银     | female |  18 | 2013-03-11 | operation                  | NULL         |   19000.00 |    403 |         3 |
    | 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                  | NULL         |   20000.00 |    403 |         3 |
    | 12 | 星星       | female |  18 | 2016-05-13 | sale                       | NULL         |    3000.29 |    402 |         2 |
    | 11 | 丁丁       | female |  18 | 2011-03-12 | sale                       | NULL         |    1000.37 |    402 |         2 |
    |  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                    | NULL         |   30000.00 |    401 |         1 |
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher                    | NULL         |    9000.00 |    401 |         1 |
    |  1 | egon       | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL         |    7300.33 |    401 |         1 |
    | 14 | 张野       | male   |  28 | 2016-03-11 | operation                  | NULL         |   10000.13 |    403 |         3 |
    | 13 | 格格       | female |  28 | 2017-01-27 | sale                       | NULL         |    4000.33 |    402 |         2 |
    |  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                    | NULL         |    2100.00 |    401 |         1 |
    | 10 | 丫丫       | female |  38 | 2010-11-01 | sale                       | NULL         |    2000.35 |    402 |         2 |
    |  9 | 歪歪       | female |  48 | 2015-03-11 | sale                       | NULL         |    3000.13 |    402 |         2 |
    |  8 | 成龙       | male   |  48 | 2010-11-11 | teacher                    | NULL         |   10000.00 |    401 |         1 |
    |  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                    | NULL         |    3500.00 |    401 |         1 |
    |  2 | alex       | male   |  78 | 2015-03-02 | teacher                    | NULL         | 1000000.31 |    401 |         1 |
    |  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                    | NULL         |    8300.00 |    401 |         1 |
    +----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    18 rows in set (0.00 sec)
    

     limit 限制条件:后面输入整数类型,只传一个值是查看记录的条数,默认从第1条记录开始查看,如果出入两个数,第一个数是初始值,就是从那一条的下一条开始查看,第二个数就是查看记录的条数。 

    mysql> select * from employee where id limit 6,10;
    +----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    | id | name   | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
    +----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    |  7 | jinxin | male   |  18 | 1900-03-01 | teacher   | NULL         | 30000.00 |    401 |         1 |
    |  8 | 成龙   | male   |  48 | 2010-11-11 | teacher   | NULL         | 10000.00 |    401 |         1 |
    |  9 | 歪歪   | female |  48 | 2015-03-11 | sale      | NULL         |  3000.13 |    402 |         2 |
    | 10 | 丫丫   | female |  38 | 2010-11-01 | sale      | NULL         |  2000.35 |    402 |         2 |
    | 11 | 丁丁   | female |  18 | 2011-03-12 | sale      | NULL         |  1000.37 |    402 |         2 |
    | 12 | 星星   | female |  18 | 2016-05-13 | sale      | NULL         |  3000.29 |    402 |         2 |
    | 13 | 格格   | female |  28 | 2017-01-27 | sale      | NULL         |  4000.33 |    402 |         2 |
    | 14 | 张野   | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
    | 15 | 程咬金 | male   |  18 | 1997-03-12 | operation | NULL         | 20000.00 |    403 |         3 |
    | 16 | 程咬银 | female |  18 | 2013-03-11 | operation | NULL         | 19000.00 |    403 |         3 |
    +----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
    10 rows in set (0.00 sec)
    

    他们的执行顺序是:from——》where—》group by—》having—》distinct—》order—》limit。如果上一个没有顺序还是不会改变

      distinct:去掉重复

    only_full_group_by:使用方法  set @@lobal sql_mode='only_full_group_by'   修改全局的一条信息

     where后面还可以跟上regexp:正则表达式条件约束,regexp后面跟的内容和正则表达式里面的内容差不多

    mysql> select * from employee where name regexp 'a.';
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex       | male   |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    |  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |    9000.00 |    401 |         1 |
    +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
    3 rows in set (0.05 sec)
     
     
    mysql> select * from employee where name regexp '^a.';
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    1 row in set (0.00 sec)
     
    mysql> select * from employee where name regexp '^j.';
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
    |  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    2 rows in set (0.00 sec)
     
    mysql> select * from employee where name regexp '^j.*n$';
    +----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
    | id | name   | sex  | age | hire_date  | post    | post_comment | salary   | office | depart_id |
    +----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
    |  7 | jinxin | male |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
    +----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
    1 row in set (0.00 sec)
     
     mysql> select * from employee where name REGEXP '^jin.*[n|g]$';
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    | id | name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
    |  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    2 rows in set (0.00 sec)
    

      

    练习题:

    USE day44;
     
    -- 1 查出所有员工的名字,薪资,格式为
    --     <名字:egon>    <薪资:3000>
    SELECT '姓名:',name,'薪资:',salary from employee;
     
    -- 2 查出所有的岗位(去掉重复)
    select distinct(post) from employee;
     
    -- 3 查出所有员工名字,以及他们的年薪,年薪的字段名为annual_year
    SELECT name,salary*12 as annual_year from employee;
     
     
     
     
    -- 1. 查看岗位是teacher的员工姓名、年龄
    select name,age from employee where post='teacher';
     
    -- 2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄
    select name,age from employee where post='teacher' and age>30;
     
    -- 3. 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资
    select name,age,salary from employee
                where post='teacher' and salary BETWEEN 9000 and 10000;
     
    -- 4. 查看岗位描述不为NULL的员工信息
    select * from employee where not null;
     
    -- 5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资
    select * from employee where post='teacher' and salary in (10000,9000,30000);
     
    -- 6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资
    select * from employee where post='teacher' and salary not in(10000,9000,30000);
     
    -- 7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪
    select * from employee where name like 'jin%';
     
     
     
    -- 1. 查询岗位名以及岗位包含的所有员工名字
    select post,group_concat(name) from employee group by post;
     
    -- 2. 查询岗位名以及各岗位内包含的员工个数
    select post,count(id) from employee group by post;
     
    -- 3. 查询公司内男员工和女员工的个数
    select sex,count(id) from employee group by sex;
     
    -- 4. 查询岗位名以及各岗位的平均薪资
    select post,avg(salary) from employee group by post;
     
    -- 5. 查询岗位名以及各岗位的最高薪资
    select post,max(salary) from employee GROUP BY post;
     
    -- 6. 查询岗位名以及各岗位的最低薪资
    select post,min(salary) from employee GROUP BY post;
     
    -- 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
    select sex,avg(salary) from employee group by sex;
     
     
     
     
    -- 1. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
    select post,group_concat(name),count(id) from employee group by post having count(id)<2;
     
    -- 3. 查询各岗位平均薪资大于10000的岗位名、平均工资
    select post,avg(salary) from employee group by post having avg(salary)>10000;
     
    -- 4. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
    SELECT post,avg(salary) from employee group by post
                    having avg(salary)>10000 and avg(salary)<20000;
     
     
     
    -- 1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
    select * from employee order by age asc,hire_date desc;
     
    -- 2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
    select post,avg(salary)as a from employee group by post
                    having avg(salary)>10000 order by a asc;
     
    -- 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
    select post,avg(salary) a from employee group by post
                            having avg(salary) >10000 order by a desc;
     
     
     
     
    -- 查看所有员工中名字是jin开头,n或者g结果的员工信息
    select * from employee where name REGEXP '^jin.*[n|g]$';
    

      

     

      

      

     

     

  • 相关阅读:
    css
    js -【 数组】判断一个变量是数组类型的几种方法
    【消灭代办】第2周
    【本周面试题】第2周
    【本周面试题】第1周
    【消灭代办】第1周
    echarts
    css
    js
    JS方法
  • 原文地址:https://www.cnblogs.com/bsxq/p/7736969.html
Copyright © 2020-2023  润新知