Million annual salary python road - MySQL The MySQL database row (record) operations (a)

MySQL row (record) operation (a)

1.Up (insert)

insert into 表名 value((字段1,字段2...);  # 只能增加一行记录
insert into 表名 values(字段1,字段2...);
insert into 表名(id,name) values(字段1,字段2),(xx1,xx2);  id,name,age

 插入查询结果
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) 
                    SELECT (字段1,字段2,字段3…字段n) FROM 表2
                    WHERE …; #将从表2里面查询出来的结果来插入到我们的表中,但是注意查询出来的数据要和我们前面指定的字段要对应好

2. 删 (delete)

delete from t3 where id = 1;删除指定的数据,删除id字段数据为1的那一行记录

清空表
delete from t3;  删除所有的数据,但是不会重置自增字段的数据号
truncate 表名;  自增字段会重置

3. 改(update)

语法:
    UPDATE 表名 SET 
        字段1=值1,  #注意语法,可以同时来修改多个值,用逗号分隔
        字段2=值2,
        WHERE CONDITION; #更改哪些数据,通过where条件来定位到符合条件的数据
        
mysql> update t2 set name='xxoo' where id = 1;
注意:不指定后面的where条件的话,会修改这个字段所有的数据
 update stu set sex = 'female', age = 18 where id < 3;

4. check (select) *****

4.1 single-table query syntax

#查询数据的本质:mysql会到你本地的硬盘上找到对应的文件,然后打开文件,按照你的查询条件来找出你需要的数据。下面是完整的一个单表查询的语法
select * from,这个select * 指的是要查询所有字段的数据。
SELECT distinct 字段1,字段2... FROM 库名.表名 #from后面是说从库的某个表中去找数据,mysql会去找到这个库对应的文件夹下去找到你表名对应的那个数据文件,找不到就直接报错了,找到了就继续后面的操作
                  WHERE 条件       #从表中找符合条件的数据记录,where后面跟的是你的查询条件
                  GROUP BY field(字段)   #分组
                  HAVING 筛选      #过滤,过滤之后执行select后面的字段筛选,就是说我要确定一下需要哪个字段的数据,你查询的字段数据进行去重,然后在进行下面的操作
                  ORDER BY field(字段)   #将结果按照后面的字段进行排序
                  LIMIT 限制条数    #将最后的结果加一个限制条数,就是说我要过滤或者说限制查询出来的数据记录的条数

Execution priority (focus) 4.2 keywords

重点中的重点:关键字的执行优先级
from
where
group by
having
select
distinct
order by
limit

  1. Locate the table: from

  2. holding where specified constraints, to the file / table records a taken

  3. a group of records retrieved group by, if there is no group by, the whole as a group

  4. The result of the grouping will be filtered having

  5. Perform select

  6. deduplication

  7. Sort the results by conditions: order by

  8. The result of limiting the number of display

4.3 simple query

#我们来创建一个员工表,然后对员工表进行一个简单的查询,来看一下效果,下面是员工表的字段
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 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
);


#查看表结构
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 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)
;

#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk

Query:

简单查询
    SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id 
    FROM employee;

    SELECT * FROM employee; #不推荐用* ,查询的时候*的效率低,至于为什么低,后面会讲到,先知道一下就行了

    SELECT name,salary FROM employee;

#避免重复DISTINCT
   SELECT post FROM employee;#直接这样查询我们会看到很多重复的内容,我只想看一下有哪些职位,那么多重复的内容是没用的,所以我们加一个去重的功能,叫做distinct 
  SELECT DISTINCT post FROM employee;  #对查询出来的记录进行去重,如果post职位有重复的,就会被剔除,剩下不重复的内容,注意,因为我们查询出来的记录里面只有一个字段post,才会根据post来进行去重
   SELECT DISTINCT post,salary FROM employee;#但是如果这样写,你会发现,貌似没有起到根据post来去重的效果,因为你的去重条件变成了post和salary两个字段的数据,只有他俩合起来是一个重复记录的时候才会去重   
   
  看一下下面这两句的效果就明白了:注意一点,使用distinct对记录进行去重的时候,distinct必须写在所有查询字段的前面,不然会报错,当然有些特别的用法可以结合着写到字段的中间或者后面,这个后面学到了我们再说
   select post,sex from employee;
   select distinct post,sex from employee;

#通过四则运算查询
    SELECT name, salary*12 FROM employee; #查询每个人的年薪,月薪我们有记录,查年薪呢?简单的乘以12就可以了,from 库.表的时候,我们已经通过use 库名;来指定了库了,所以from的时候直接写from 表,就行了
    #你会发现,结果是出来了,但是我们的那个薪资的字段名变成了salary*12,是因为我们通过查询语句查询出来的也是一张表,但是这个表是不是内存当中的一个虚拟表,并不是我们硬盘中存的那个完整的表,对吧,虚拟表是不是也有标题和记录啊,既然是一个表,我们是可以指定这个虚拟表的标题的,通过as+新字段名来指定

    SELECT name, salary*12 AS Annual_salary FROM employee; #as + 新字段名,就是起一个别名的意思,上面的那个salary*12的字段名也是一个别名,只不过不直观,是mysql自动给你写上的
    SELECT name, salary*12 Annual_salary FROM employee;
  #除了乘法以外,加减乘除都是可以的


#自定义显示格式,自己规定查询结果的显示格式
   CONCAT() 函数用于连接字符串
   SELECT CONCAT('姓名: ',name,'  年薪: ', salary*12)  AS Annual_salary  #我想让name这个字段显示的字段名称是中文的姓名,让salary*12显示的是中文的年薪,
   FROM employee;#看结果:通过结果你可以看出,这个concat就是帮我们做字符串拼接的,并且拼接之后的结果,都在一个叫做Annual_salary的字段中了
    +---------------------------------------+
    | Annual_salary |
    +---------------------------------------+
    | 姓名: egon 年薪: 87603.96 |
    | 姓名: alex 年薪: 12000003.72 |
    | 姓名: wupeiqi 年薪: 99600.00 |
    | 姓名: yuanhao 年薪: 42000.00 |

    .....

       +---------------------------------------+

   SELECT CONCAT('姓名: ',name,'  年薪: ', salary*12)  AS Annual_salary,CONCAT('性别:',sex) from employee;#还可以这样分成两列  


   CONCAT_WS() 第一个参数为分隔符来进行字符串拼接
   SELECT CONCAT_WS(':',name,salary*12)  AS Annual_salary  #通过冒号来将name和salary连接起来
   FROM employee;
   #上面这个效果我们也可以通过concat来实现:SELECT CONCAT(name,':',salary*12)  AS Annual_salary from employee;
   结合CASE语句:结合条件来对查询的结果进行一些加工操作
   SELECT
       (
           CASE
           WHEN NAME = 'egon' THEN
               NAME
           WHEN NAME = 'alex' THEN
               CONCAT(name,'_BIGSB')
           ELSE
               concat(NAME, 'SB')
           END
       ) as new_name,sex
   FROM
       employee;

#看结果:
    +--------------+--------+
    | new_name | sex |
    +--------------+--------+
    | egon | male |
    | alex_BIGSB | male |
    | wupeiqiSB | male |
    | yuanhaoSB | male |
    | liwenzhouSB | male |
    | jingliyangSB | female |
    | jinxinSB | male |
    | 成龙SB | male |

    ...

    +--------------+

4.4 where screening

where statements can be used:

    Before we use the back where the sentence with id = 1 is not this type of ah, = number of connections, in addition to the = sign, and use the other to see the following:

1. 比较运算符:> < >= <= <> !=
    SELECT name FROM employee WHERE post='sale'; 

2. between 10 and 15  id值在10到15之间 #大于等于和小于等于的区间
    mysql> select * from employee where id between 10 and 15
3. in(1,3,6)  值是80或90或100
    select * from employee where id in(1,3,6)  等价于id=1 or id=3 or id=6;
4. like 'egon%'
      pattern可以是%或_,
      %表示任意多字符
        select * from employee where name like "wu%";
      _表示一个字符 
        select * from employee where name like "al_";结果空
        select * from employee where name like "al__";#结果alex
        mysql> select * from employee where name like "al___"; 三个下划线的无法匹配到alex,结果为空

5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
    select * from employee id>10 and name like "al%";
    select * from employee not id>10;id小于等于10的,not取反
#1:单条件查询
    SELECT name FROM employee
        WHERE post='sale';  #注意优先级,我们说where的优先级是不是比select要高啊,所以我们的顺序是先找到这个employee表,然后按照post='sale'的条件,然后去表里面select数据
        
#2:多条件查询
    SELECT name,salary FROM employee 
        WHERE post='teacher' AND salary>10000;

#3:关键字BETWEEN AND 写的是一个区间
    SELECT name,salary FROM employee 
        WHERE salary BETWEEN 10000 AND 20000; #就是salary>=10000 and salary<=20000的数据

    SELECT name,salary FROM employee 
        WHERE salary NOT BETWEEN 10000 AND 20000; #加个not,就是不在这个区间内,薪资小于10000的或者薪资大于20000的,注意没有等于,
    
#4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS) 判断null只能用is
    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NULL;

    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NOT NULL;
        
    SELECT name,post_comment FROM employee 
        WHERE post_comment=''; 注意''是空字符串,不是null,两个是不同的东西,null是啥也没有,''是空的字符串的意思,是一种数据类型,null是另外一种数据类型
    ps:
        执行
        update employee set post_comment='' where id=2;
        再用上条查看,就会有结果了

#5:关键字IN集合查询
    SELECT name,salary FROM employee 
        WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; #这样写是不是太麻烦了,写一大堆的or,下面我们用in这个简单的写法来搞
    
    SELECT name,salary FROM employee 
        WHERE salary IN (3000,3500,4000,9000) ;

    SELECT name,salary FROM employee 
        WHERE salary NOT IN (3000,3500,4000,9000) ;

#6:关键字LIKE模糊查询,模糊匹配,可以结合通配符来使用
    通配符’%’  #匹配任意所有字符
    SELECT * FROM employee 
            WHERE name LIKE 'eg%';

    通配符’_’  #匹配任意一个字符   
    SELECT * FROM employee 
            WHERE name LIKE 'al__'; #注意我这里写的两个_,用1个的话,匹配不到alex,因为al后面还有两个字符ex。

where we say finished condition, where the condition in the end how this works, look at us: We select id, name, age from employee where id> 7; This statement is about

  After the first first find the employee table, find the table, mysql will hold back where constraints to table inside to find qualified data, and then through all the data in your table, check whether id is greater than 7, one by one comparison, and then if they find the id than seven, it would to select the entire record, but I say just take select id, name, age three fields inside of this data, and then print the data of these three fields, then continue where filtered down to see if there id is not greater than 7, and then found a qualifying gave a select, then repeat this sort of thing, until all the data is filtered again before the end. This is a way where working conditions.

4.5 Packet query (group by)

  • What is a packet? Why packets?
    #1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的
    
    #2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等
    
    #3、为何要分组呢?是因为我们有时候会需要以组为单位来统计一些数据或者进行一些计算的,对不对,比方说下面的几个例子
        取每个部门的最高工资  
        取每个部门的员工数
        取男人数和女人数  
    
        小窍门:‘每’这个字后面的字段,就是我们分组的依据,只是个小窍门,但是不能表示所有的情况,看上面第三个分组,没有'每'字,这个就需要我们通过语句来自行判断分组依据了
        我们能用id进行分组吗,能,但是id是不是重复度很低啊,基本没有重复啊,对不对,这样的字段适合做分组的依据吗?不适合,对不对,依据性别分组行不行,当然行,因为性别我们知道,是不是就两种啊,也可能有三种是吧,这个重复度很高,对不对,分组来查的时候才有更好的意义
      
    #4、大前提:
        可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数
    
    #注意一点,在查询语句里面select 字段 from 表,这几项是必须要有的,其他的什么where、group by等等都是可有可无的
    

    ##### Test

    mysql> select * from employee group by post;
    +----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    | id | name   | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
    +----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    | 14 | 张野   | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
    |  9 | 歪歪   | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
    |  2 | alex   | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
    |  1 | egon   | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              | NULL         |    7300.33 |    401 |         1 |
    +----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
    4 rows in set (0.06 sec)
    
    通过结果可以看出,如果直接通过post部门字段来进行分组,默认拿到的结果都是每组的第一条数据
    但是你想,我们分组的意义是什么,是不是说通过分组来统计一下整个组的情况啊,不再是看某个人单独的情况了,对不对,并且将来你在这样进行直接分组查询的时候,可能因为你们公司设置的mysql的环境不同,而查不到数据,我们可以看到,我们现在仍然可以查询出来数据,但是如果我们在sql_mode中添加了下面的only_full_group_by这个mode,那么我们在直接分组查询,就无法得到数据了,只能得到字段名
    并且设置了sql_mode为only_full_group_by之后,select *,就不行了,会直接报错,只能select post ,post是你分组的那个字段
    
  • ONLY_FULL_GROUP_BY
    #查看MySQL 5.7默认的sql_mode如下:
    mysql> select @@global.sql_mode;
    ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    
    #!!!注意
    ONLY_FULL_GROUP_BY的语义就是确定select target list中的所有列的值都是明确语义,简单的说来,在ONLY_FULL_GROUP_BY模式下,target list中的值要么是来自于聚集函数的结果,要么是来自于group by list中的表达式的值。
    
    
    #设置sql_mole如下操作(我们可以去掉ONLY_FULL_GROUP_BY模式):
    mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
    

    test

    mysql> select @@global.sql_mode;
    +-------------------+
    | @@global.sql_mode |
    +-------------------+
    |                   |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from emp group by post; 
    +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    | id | name | sex    | age | hire_date  | post                       | post_comment | salary     | office | depart_id |
    +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    | 14 | 张野 | male   |  28 | 2016-03-11 | operation                  | NULL         |   10000.13 |    403 |         3 |
    |  9 | 歪歪 | female |  48 | 2015-03-11 | sale                       | NULL         |    3000.13 |    402 |         2 |
    |  2 | alex | male   |  78 | 2015-03-02 | teacher                    | NULL         | 1000000.31 |    401 |         1 |
    |  1 | egon | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL         |    7300.33 |    401 |         1 |
    +----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
    4 rows in set (0.00 sec)
    
    
    #由于没有设置ONLY_FULL_GROUP_BY,于是也可以有结果,默认都是组内的第一条记录,但其实这是没有意义的
    
    mysql> set global sql_mode='ONLY_FULL_GROUP_BY';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit #设置成功后,一定要退出,然后重新登录方可生效
    Bye
    
    mysql> use db1;
    Database changed
    mysql> select * from emp group by post; #报错
    ERROR 1055 (42000): 'db1.emp.id' isn't in GROUP BY  #意思是告诉你,你select后面取的字段必须在你的group by后面的字段里面才行
    
    mysql> select post,count(id) from emp group by post; #只能查看分组依据和使用聚合函数
    +----------------------------+-----------+
    | post                       | count(id) |
    +----------------------------+-----------+
    | operation                  |         5 |
    | sale                       |         5 |
    | teacher                    |         7 |
    | 老男孩驻沙河办事处外交大使 |         1 |
    +----------------------------+-----------+
    4 rows in set (0.00 sec)
    
    因为一般分组之后,我们再考虑其中一条数据就没有什么意义了,所以一般我们都会在这种模式下进行分组,下面我们在看看group by,下面的内容
    
    
  • GROUP BY
    单独使用GROUP BY关键字分组
        SELECT post FROM employee GROUP BY post;
        注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
    
    GROUP BY关键字和GROUP_CONCAT()函数一起使用,比如说我想按部门分组,每个组有哪些员工,都显示出来,怎么搞
        SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照岗位分组,并查看组内所有成员名,通过逗号拼接在一起
        SELECT post,GROUP_CONCAT(name,':',salary) as emp_members FROM employee GROUP BY post;
    
    GROUP BY一般都会与聚合函数一起使用,聚合是什么意思:聚合就是将分组的数据聚集到一起,合并起来搞事情,拿到一个最后的结果
        select post,count(id) as count from employee group by post;#按照岗位分组,并查看每个组有多少人,每个人都有唯一的id号,我count是计算一下分组之后每组有多少的id记录,通过这个id记录我就知道每个组有多少人了
    
    关于集合函数,mysql提供了以下几种聚合函数:count、max、min、avg、sum等,上面的group_concat也算是一个聚合函数了,做字符串拼接的操作
    

    Emphasize

    如果我们用设置了unique约束的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
    多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
    
  • Aggregate function
    #强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组
    
    示例:
        SELECT COUNT(*) FROM employee;  #count是统计个数用的
        SELECT COUNT(*) FROM employee WHERE depart_id=1;  #后面跟where条件的意思是统计一下满足depart_id=1这个的所有记录的个数
        SELECT MAX(salary) FROM employee;  #max()统计分组后每组的最大值,这里没有写group by,那么就是统计整个表中所有记录中薪资最大的,薪资的值
        SELECT MIN(salary) FROM employee;
        SELECT AVG(salary) FROM employee;
        SELECT SUM(salary) FROM employee;
        SELECT SUM(salary) FROM employee WHERE depart_id=3;
    

4.6 having filter

HAVING and WHERE is not the same place !!!!!!

having的语法格式和where是一模一样的,只不过having是在分组之后进行的进一步的过滤,where不能使用聚合函数,having是可以使用聚合函数的
#!!!执行优先级从高到低:where > group by > having 
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。

#2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,having是可以使用聚合函数
test
来个需求:统计各部门年龄在30岁及以上的员工的平均薪资,并且保留平均工资大于10000的部门
答案:select post,avg(salary) as new_sa from employee where age>=30 group by post having avg(salary) > 10000;
看结果:
  +---------+---------------+
  | post | new_sa |
  +---------+---------------+
  | teacher | 255450.077500 |
  +---------+---------------+
  1 row in set (0.00 sec)

然后我们看这样一句话:select * from employee having avg(salary) > 10000;
只要一运行就会报错:
    mysql> select * from employee having avg(salary) > 10000;
    ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

是因为having只能在group by后面运行
Talk about de-emphasis: distinct

    The results of a query to re-: select distinct post from employee; important to note distinct wrote in a previous query field, or will be error, on other issues when using the distinct look at the following summary

有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段,distinct 想写在其他字段后面需要配合聚合函数来写。

mysql> select id,count(distinct post) from employee;
ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
报错了:是因为distinct不能返回其他的字段,只能返回目标字段
mysql> select count(distinct post) from employee;
+----------------------+
| count(distinct post) |
+----------------------+
|                    4 |
+----------------------+
1 row in set (0.00 sec)

4.7 Query sort (order by)

按单列排序
    SELECT * FROM employee ORDER BY salary; #默认是升序排列
    SELECT * FROM employee ORDER BY salary ASC; #升序
    SELECT * FROM employee ORDER BY salary DESC; #降序
    
按多列排序:先按照age升序,如果年纪相同,则按照薪资降序
    SELECT * from employee
        ORDER BY age, #注意排序的条件用逗号分隔
        salary DESC;

4.8 limit the query number of records (limit)

Commonly used in the following two situations:

  1. How many rows of data required before
  2. Paging
示例:
  #取出工资最高的前三位
    SELECT * FROM employee ORDER BY salary DESC 
        LIMIT 3;                    #默认初始位置为0,从第一条开始顺序取出三条 
    
    SELECT * FROM employee ORDER BY salary DESC
        LIMIT 0,5; #从第0开始,即先查询出第一条,然后包含这一条在内往后查5条 

    SELECT * FROM employee ORDER BY salary DESC
        LIMIT 5,5; #从第5开始,即先查询出第6条,然后包含这一条在内往后查5条

4.9 Regular Expressions query

#之前我们用like做模糊匹配,只有%和_,局限性比较强,所以我们说一个正则,之前我们是不是学过正则匹配,你之前学的正则表达式都可以用,正则是通用的
SELECT * FROM employee WHERE name REGEXP '^ale';

SELECT * FROM employee WHERE name REGEXP 'on$';

SELECT * FROM employee WHERE name REGEXP 'm{2}';


小结:对字符串匹配的方式
WHERE name = 'egon';
WHERE name LIKE 'yua%';
WHERE name REGEXP 'on$';

Small Exercise:

查看所有员工中名字是jin开头,n或者g结果的员工信息
select * from employee where name regexp '^jin.*[g|n]$';

Guess you like

Origin www.cnblogs.com/zhangchaoyin/p/11448006.html