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

mysql 数据库 IV(单表查询)

时间:2020-01-10 12:15:00      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:ica   rom   table   char   mysql   mysql 数据库   each   ted   duplicate   

1.今日内容

  1. 单表查询语法

    select distinct 字段1,字段2...  from 表名
                              where 条件
                                  group by 字段
                              having 筛选
                                  order by 字段
                              limit 限制条数
  2. 关键字执行的优先级

    from       # 找到表
    where      # 拿着where指定的约束条件,去文件/表中取出一条条记录
    group by   # 将取出来的数据进行group by,如果没有group by,则整体做为一组
    select     
    distinct   #执行select-去重
    having     #将分组的结果进行having过滤
    order by   #将结果按条件排序,order by
    limit      #限制结果的显示条数

2.具体内容

  1. 简单查询


    建表准备工作

    mysql
       #创建表
     mysql> create table employee(                                                                   
          -> id int not null unique auto_increment,
         -> emp_name varchar(20) not null,
          -> sex enum('male','female') not null default 'female',
          -> 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.04 sec)
    
    
      #查看表结构
    
     mysql> desc employee;
     +--------------+-----------------------+------+-----+---------+---  -------------+
     | Field        | Type                  | Null | Key | Default |         Extra          |
     +--------------+-----------------------+------+-----+---------+---  -------------+
     | id           | int(11)               | NO   | PRI | NULL    |         auto_increment |
     | emp_name     | varchar(20)           | NO   |     | NULL    |                  |
     | sex          | enum('male','female') | NO   |     | female  |                  |
     | 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.02 sec)
    
    
       #插入数据
    
     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)
                 -> ;
         Query OK, 18 rows affected (0.01 sec)
         Records: 18  Duplicates: 0  Warnings: 0
    
    
     mysql> select * from employee;
     +----+------------+--------+-----+------------+-------------------  ----------------------+--------------+------------+--------+------  -----+
     | 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 |
     |  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 |
     | 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 |
     | 17 | 程咬铜     | male   |  18 | 2015-04-11 | operation                                   | NULL         |   18000.00 |    403 |         3 |
     | 18 | 程咬铁     | female |  18 | 2014-05-12 | operation                                   | NULL         |   17000.00 |    403 |         3 |
     +----+------------+--------+-----+------------+-------------------  ----------------------+--------------+------------+--------+------  -----+
     18 rows in set (0.00 sec)
    
    

mysql 数据库 IV(单表查询)

标签:ica   rom   table   char   mysql   mysql 数据库   each   ted   duplicate   

原文地址:https://www.cnblogs.com/xiaohei-chen/p/12175498.html

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