zoukankan      html  css  js  c++  java
  • 数据操作

    mysql> create table employee(
        -> id int primary key auto_increment,
        -> emp_name char(12) not null,
        -> sex enum('male','female') not null default 'male', #大部分是男的
        -> age int(3) unsigned not null default 28,
        -> hire_date date not null,
        -> post char(15),
        -> post_comment varchar(100),
        -> salary float(15,2),
        -> office int, #一个部门一个屋子
        -> depart_id int
        -> );
    mysql> insert into employee(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)
        -> ;
    数据准备

    insert into 表名 values (数据)
    insert into(字段名) 表名 values (数据);

    delete from 表名 where 条件;   删除符合条件的数据
    delete from 表名;              清空表

    update 表名 set 字段名=值 where 条件;
    update 表名 set 字段名1=值1,字段名2=值2 where 条件;   改变符合条件的两个字段的值

    select关键字

    select * from 表名;                 查询所有内容
    select 字段1,字段2 from 表名;        查找指定列的内容
    select distinct 字段名 from 表名;    去重显示
    select 字段*12 from 表名;           做四则运算
    select 字段 as 新字段名 from 表名;    重命名   没有更改原表的字段,只是在本次查询中显示新的字段名
    concat   concat_ws                 拼接  
    
    case 开始一个条件语句 when 条件1 then 字段操作 when 条件2 then 字段操作
    else 字段操作 end 结束条件语句
    # 去重显示所有的部门
    mysql> select distinct post from employee;
    +-----------------------------------------+
    | post                                    |
    +-----------------------------------------+
    | 老男孩驻沙河办事处外交大使              |
    | teacher                                 |
    | sale                                    |
    | operation                               |
    +-----------------------------------------+
    4 rows in set (0.00 sec)
    
    # 计算出每个人的年薪,并将字段名显示为年薪
    mysql> select emp_name,salary*12 as annual_year from employee;
    +------------+-------------+
    | emp_name   | annual_year |
    +------------+-------------+
    | egon       |    87603.96 |
    | alex       | 12000003.75 |
    | wupeiqi    |    99600.00 |
    | yuanhao    |    42000.00 |
    | liwenzhou  |    25200.00 |
    | jingliyang |   108000.00 |
    | jinxin     |   360000.00 |
    | 成龙       |   120000.00 |
    | 歪歪       |    36001.56 |
    | 丫丫       |    24004.20 |
    | 丁丁       |    12004.44 |
    | 星星       |    36003.48 |
    | 格格       |    48003.96 |
    | 张野       |   120001.56 |
    | 程咬金     |   240000.00 |
    | 程咬银     |   228000.00 |
    | 程咬铜     |   216000.00 |
    | 程咬铁     |   204000.00 |
    +------------+-------------+
    18 rows in set (0.00 sec)
    
    # 按照一定格式拼接显示
    mysql> select concat('<姓名:',emp_name,'>  <薪资:',salary,'>') from employee;
    +------------------------------------------------------+
    | concat('<姓名:',emp_name,'>  <薪资:',salary,'>')     |
    +------------------------------------------------------+
    | <姓名:egon>  <薪资:7300.33>                          |
    | <姓名:alex>  <薪资:1000000.31>                       |
    | <姓名:wupeiqi>  <薪资:8300.00>                       |
    | <姓名:yuanhao>  <薪资:3500.00>                       |
    | <姓名:liwenzhou>  <薪资:2100.00>                     |
    | <姓名:jingliyang>  <薪资:9000.00>                    |
    | <姓名:jinxin>  <薪资:30000.00>                       |
    | <姓名:成龙>  <薪资:10000.00>                         |
    | <姓名:歪歪>  <薪资:3000.13>                          |
    | <姓名:丫丫>  <薪资:2000.35>                          |
    | <姓名:丁丁>  <薪资:1000.37>                          |
    | <姓名:星星>  <薪资:3000.29>                          |
    | <姓名:格格>  <薪资:4000.33>                          |
    | <姓名:张野>  <薪资:10000.13>                         |
    | <姓名:程咬金>  <薪资:20000.00>                       |
    | <姓名:程咬银>  <薪资:19000.00>                       |
    | <姓名:程咬铜>  <薪资:18000.00>                       |
    | <姓名:程咬铁>  <薪资:17000.00>                       |
    +------------------------------------------------------+
    18 rows in set (0.00 sec)
    示例

    where条件

    对值的判断  
    = > < != <> >= <=
    对范围的判断 
    between 小的值 and 大的值   上下包含
    is null   判断是不是为空
    in (值1,值2,值3,值4)
    模糊匹配   like
       %是通配符,匹配任意长度的任意内容
          'a%'   查找以a开头的 
          '%a'   查找以a结尾的
          '%a%'  查找中间包含a的
       _ 匹配任意一个长度的内容
          '_a'  匹配任意一个字符+a
          'a__'  以a开头,后面是任意两个字符
    逻辑运算符   and  or not
    # 筛选出老师中年龄超过30岁的姓名,年龄,薪资
    mysql> select emp_name,age,salary from employee where post='teacher'and age>30;;
    +----------+-----+------------+
    | emp_name | age | salary     |
    +----------+-----+------------+
    | alex     |  78 | 1000000.31 |
    | wupeiqi  |  81 |    8300.00 |
    | yuanhao  |  73 |    3500.00 |
    | 成龙     |  48 |   10000.00 |
    +----------+-----+------------+
    4 rows in set (0.00 sec)
    
    # 筛选出老师中薪资在9000到10000之间的姓名,年龄,薪资
    mysql> select emp_name,age,salary from employee where post='teacher'and salary between 9000 and 10000;
    +------------+-----+----------+
    | emp_name   | age | salary   |
    +------------+-----+----------+
    | jingliyang |  18 |  9000.00 |
    | 成龙       |  48 | 10000.00 |
    +------------+-----+----------+
    2 rows in set (0.00 sec)
    
    # 筛选出工资是10000或者9000或者3000的员工姓名,年龄,薪资
    mysql> select emp_name,age,salary from employee where salary in (10000,9000,30000);
    +------------+-----+----------+
    | emp_name   | age | salary   |
    +------------+-----+----------+
    | jingliyang |  18 |  9000.00 |
    | jinxin     |  18 | 30000.00 |
    | 成龙       |  48 | 10000.00 |
    +------------+-----+----------+
    3 rows in set (0.00 sec)
    
    # 筛选名字以'jin'开头的姓名和年薪
    mysql> select emp_name,salary*12 as annual_salary from employee where emp_name like 'jin%';
    +------------+---------------+
    | emp_name   | annual_salary |
    +------------+---------------+
    | jingliyang |     108000.00 |
    | jinxin     |     360000.00 |
    +------------+---------------+
    2 rows in set (0.00 sec)
    示例

    group by     分组

    select 字段名 from 表名 where 条件 group by 字段名; #分成几个组 就显示几条数据       需要用到group_concat

    # 按照部门分组,统计每个部门人的姓名
    mysql> select post,group_concat(emp_name) from employee group by post;
    +-----------------------------------------+---------------------------------------------------------+
    | post                                    | group_concat(emp_name)                                  |
    +-----------------------------------------+---------------------------------------------------------+
    | operation                               | 程咬铁,程咬铜,程咬银,程咬金,张野                        |
    | sale                                    | 格格,星星,丁丁,丫丫,歪歪                                |
    | teacher                                 | 成龙,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex   |
    | 老男孩驻沙河办事处外交大使              | egon                                                    |
    +-----------------------------------------+---------------------------------------------------------+
    4 rows in set (0.00 sec)
    示例

    聚合函数

    count() 统计有多少条记录是符合条件的 统计字段内容不能为空,如果为空,不进行累加
    min(字段名)
    max(字段名)
    avg(字段名)
    sum(字段名)
    # 统计出男性和女性的个数
    mysql> select sex,count(sex) from employee group by sex;
    +--------+------------+
    | sex    | count(sex) |
    +--------+------------+
    | male   |         10 |
    | female |          8 |
    +--------+------------+
    2 rows in set (0.00 sec)
    
    # 计算男性和女性的平均薪资
    mysql> select sex,avg(salary) from employee group by sex;
    +--------+---------------+
    | sex    | avg(salary)   |
    +--------+---------------+
    | male   | 110920.077246 |
    | female |   7250.183746 |
    +--------+---------------+
    2 rows in set (0.00 sec)
    示例

    分组 + 聚合函数

    聚合函数只是代表一列的结果,并不能直接将这一列相关的一行数据直接取到

    #求各部门平均薪资
    mysql> select post,avg(salary) from employee group by post;
    +-----------------------------------------+---------------+
    | post                                    | avg(salary)   |
    +-----------------------------------------+---------------+
    | operation                               |  16800.025977 |
    | sale                                    |   2600.293994 |
    | teacher                                 | 151842.901786 |
    | 老男孩驻沙河办事处外交大使              |   7300.330078 |
    +-----------------------------------------+---------------+
    4 rows in set (0.12 sec)
    示例

    having    过滤

    是根据分组的结果进行过滤的,对一个组的数据进行进一步筛选的时候,使用having关键字,对条件的使用,应该尽量使用where进行单条数据的筛选

    # 查询各岗位平均薪资大于10000的岗位名、平均工资
    mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000;
    +-----------+---------------+
    | post      | avg(salary)   |
    +-----------+---------------+
    | operation |  16800.025977 |
    | teacher   | 151842.901786 |
    +-----------+---------------+
    2 rows in set (0.01 sec)
    示例

    order by 排序  

    先根据条件找到所有符合条件的行,对这些行中的某一个字段对这些信息进行排序,默认升序   asc          最后写desc   降序

    # 按照年龄升序排列
    mysql> select * from employee order by age;
    +----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    | id | emp_name   | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
    +----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    |  1 | egon       | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              | NULL         |    7300.33 |    401 |         1 |
    | 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 |
    | 18 | 程咬铁     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
    |  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 |
    | 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
    | 14 | 张野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
    |  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)
    
    # 按照薪资降序排列
    mysql> select * from employee order by salary desc;
    +----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    | id | emp_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 |
    |  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.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 |
    | 14 | 张野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
    |  8 | 成龙       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
    |  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
    |  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
    |  1 | egon       | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              | NULL         |    7300.33 |    401 |         1 |
    | 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
    |  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
    | 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
    |  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    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 |
    | 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
    +----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    18 rows in set (0.00 sec)
    示例

    limit  限制查询的记录数

    limit   限制查询的记录数
    limit n    取前n个值
    limit m,n  从m+1的位置开始,取n条数据     应用: 分页显示
    limit n offset m    从m+1的位置开始,取n条数据
    # 分页显示  每页显示5条数据
    mysql> select * from employee limit 5;
    +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    | id | emp_name  | sex  | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
    +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    |  1 | egon      | male |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              | NULL         |    7300.33 |    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 |
    |  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 |
    +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    5 rows in set (0.01 sec)
    
    mysql> select * from employee limit 5,5;
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    | id | emp_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 |
    |  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 |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
    5 rows in set (0.00 sec)
    示例

    关键字执行顺序 : from > where > group by > select > having >order by > limit

  • 相关阅读:
    misc子系统
    Spring boot+RabbitMQ环境
    Linux input
    lnmp环境的搭建
    DDD的.NET开发框架
    【踩坑记】从HybridApp到ReactNative
    Migrating from IntelliJ Projects
    Windows下Redis中RedisQFork位置调整
    远程仓库版本回退方法 good
    maven repository
  • 原文地址:https://www.cnblogs.com/sandy-123/p/10498223.html
Copyright © 2011-2022 走看看