• MySQL高级语句


    目录

    MySQL高级语句

    1、select——显示表格中一个或多个栏位的数据

     2、distinct——不显示重复的资料

     3、where——有条件查询

     4、and 、or——且、或

     6、between——显示两个值范围内的资料

     7、通配符

    8、like——匹配一个模式来找出我们的数据

    9、order by——按关键字排序

    10、函数

    (1) 数学函数

    (2) 聚合函数

    (3)字符串函数

    字段截取 

     11、GROUP BY------对GROUP BY后面的栏位的查询结果进行汇总分组

     格式:

     12、HAVING------用来过滤由GROUP BY 语句返回的记录表

    格式:

     13、别名------栏位別名,表格別名

     14、子查询:连接表格

    格式:

     15、EXISTS------用来测试内查询有没有产生任何结果

    连接查询

     1、inner join(等值相连)

    格式:

     2、left join(左联接)

    格式:

    3、right join(右联接)

    格式:

    CREATE VIEW 视图

    格式:

    MySQL之联集、交集值、无交集值、case

    1、联集

    (1)UNION

     (2)UNION ALL

     ​编辑

     2、交集值

     3、无交集值

     MySQL之正则表达式

    格式:


    MySQL高级语句

    1、select——显示表格中一个或多个栏位的数据

    格式

    1. select "栏位" from "表名";
    2. 例:
    3. select place_name from destination;

    例:

     2、distinct——不显示重复的资料

    格式:

    1. select distinct "栏位" from "表名";
    2. 例:
    3. select distinct place_name from info;

     例:

     3、where——有条件查询

    格式:

    1. select "栏位" from "表名" where "条件";
    2. 例:
    3. select place_name from info where sales > 300;

    例:

     4、and 、or——且、或

     格式:

    1. select "栏位" from "表名" where "条件1" {[and|or] "条件2"}+ ;
    2. 例:
    3. select place_name from info where sales > 350 or sales < 300;
    4. select place_name from info where sales > 300 and sales < 350;

    例:

     5、in——显示已知的值的资料

    1. select "栏位" from "表名" where "栏位" in ('值1', '值2', ...);
    2. 例:
    3. select * from info where place_name in ('beijing');
    4. select * from info where place_name in ('beijing','kunming');

    例:

     6、between——显示两个值范围内的资料

    1. select "栏位" from "表名" where "栏位" between '值1' and '值2';
    2. 例:
    3. select * from info where sales between '300' and '350';

    例:

     7、通配符

    Tip:通常通配符都是跟like一起使用的

    1. % :百分号表示零个、一个或多个字符
    2. _ :下划线表示单个字符
    3. 例:
    4. 'A_Z':所有以 'A' 起头,另一个任何值的字符,且以 'Z' 为结尾的字符串。例如,'ABZ''A2Z' 都符合这一个模式,而 'AKKZ' 并不符合 (因为在 A 和 Z 之间有两个字符,而不是一个字符)。
    5. 'ABC%': 所有以 'ABC' 起头的字符串。例如,'ABCD''ABCABC' 都符合这个模式。
    6. '%XYZ': 所有以 'XYZ' 结尾的字符串。例如,'WXYZ''ZZXYZ' 都符合这个模式。
    7. '%AN%': 所有含有 'AN'这个模式的字符串。例如,'LOS ANGELES''SAN FRANCISCO' 都符合这个模式。
    8. '_AN%':所有第二个字母为 'A' 和第三个字母为 'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而 'LOS ANGELES' 则不符合这个模式。

    8、like——匹配一个模式来找出我们的数据

    格式:

    1. select "栏位" from "表名" where "栏位" like {模式};
    2. 例:
    3. select * from info where place_name like '_ei%';

    9、order by——按关键字排序

     格式:

    1. select "栏位" from "表名" [where "条件"] order by "栏位" [asc, desc];
    2. #ASC 是按照升序进行排序的,是默认的排序方式。
    3. #DESC 是按降序方式进行排序。
    4. 例:
    5. select place_name,sales,date from info order by sales;
    6. select place_name,sales,date from info order by sales asc;
    7. select place_name,sales,date from info order by sales desc;

    例:

    10、函数

    (1) 数学函数

    1. abs(x) 返回 x 的绝对值
    2. rand() 返回 01 的随机数
    3. mod(x,y) 返回 x 除以 y 以后的余数
    4. power(x,y) 返回 x 的 y 次方
    5. round(x) 返回离 x 最近的整数
    6. round(x,y) 保留 x 的 y 位小数四舍五入后的值
    7. sqrt(x) 返回 x 的平方根
    8. truncate(x,y) 返回数字 x 截断为 y 位小数的值
    9. ceil(x) 返回大于或等于 x 的最小整数
    10. floor(x) 返回小于或等于 x 的最大整数
    11. greatest(x1,x2…) 返回集合中最大的值
    12. least(x1,x2…) 返回集合中最小的值
    13. select abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
    14. select round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);

    例:

    (2) 聚合函数

    1. avg() 返回指定列的平均值
    2. count() 返回指定列中非 NULL 值的个数
    3. min() 返回指定列的最小值
    4. max() 返回指定列的最大值
    5. sum(x) 返回指定列的所有值之和
    6. #count(*)包括了所有的列的行数,在统计结果的时候,不会忽略值为NULL
    7. #count(列名)只包括列名那一列的行数,在统计结果的时候,会忽略列值为NULL的行
    8. 例:
    9. select avg(sales) from info;
    10. select count(place_name) from info;
    11. select count(distinct place_name) from info;
    12. select max(sales) from info;
    13. select min(sales) from info;
    14. select sum(sales) from info;

    求表中数据的平均值

    查看表info中place_name列有多少数据

     先对info表中place_name列进行去重,再统计行数,下图中place_name有两个beijing,去重后只剩一个beijing,再加上kunming和chengdu两个值,那么就剩下3行数据

     分别查出表info中sales列的最大值和最小值

    求和

    (3)字符串函数

    1. trim() 返回去除指定格式的值
    2. concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
    3. substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
    4. substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串
    5. length(x) 返回字符串 x 的长度
    6. replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y
    7. upper(x) 将字符串 x 的所有字母变成大写字母
    8. lower(x) 将字符串 x 的所有字母变成小写字母
    9. left(x,y) 返回字符串 x 的前 y 个字符
    10. right(x,y) 返回字符串 x 的后 y 个字符
    11. repeat(x,y) 将字符串 x 重复 y 次
    12. space(x) 返回 x 个空格
    13. strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
    14. reverse(x) 将字符串 x 反转

    先查看配置文件是否开启了PIPES_AS_CONCAT,如果开启了PIPES_AS_CONCAT,则"||"视为字符串的连接操作符而非或运算符

    •  trim() 返回去除指定格式的值
    1. select trim([ [位置] [要移除的字符串] from ] 字符串);
    2. #[位置]:的值可以是开头,结尾或者开头及结尾。
    3. #[要移除的字符串]:从字符串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
    4. 例:
    5. select trim(leading '123' from '123456abc');

    例:

    使用trim去除123456abc前面的123字符 

    • concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
    1. select concat(region, place_name) from destination;
    2. select region || ' ' || place_name from destination;
    3. select region || ' ' || place_name from destination where place_name = 'beijing';

     

    字段截取 

    • substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
    • substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串
    1. select substr(place_name,3) from destination where place_name = 'beijing';
    2. select substr(place_name,2,4) from destination where place_name = 'beijing';

    • length(x) 返回字符串 x 的长度
    select region,length(place_name) from destination;
    

    • replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y
    select replace(region,'China',' ') from destination;
    

    • upper(x) 将字符串 x 的所有字母变成大写字母
    • lower(x) 将字符串 x 的所有字母变成小写字母
    1. select upper(region) from destination;
    2. select lower(region) from destination;

     

    • left(x,y) 返回字符串 x 的前 y 个字符
    • right(x,y) 返回字符串 x 的后 y 个字符
    1. select left(region,5) from destination;
    2. select right(region,5) from destination;

    • repeat(x,y) 将字符串 x 重复 y 次
    1. select repeat(region,2) from destination;
    2. select repeat(123,2);

    •  space(x) 返回 x 个空格

    •  strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
    1. select strcmp(2,3);
    2. select strcmp(3,3);
    3. select strcmp(4,3);
    4. select strcmp(5,3);

    •  reverse(x) 将字符串 x 反转
    1. select reverse(1234567);
    2. select reverse('abcdefg');

     11、GROUP BY------对GROUP BY后面的栏位的查询结果进行汇总分组

    • 通常是结合聚合函数一起使用的
    • GROUP BY 有一个原则,就是 SELECT 后面的所有列中,没有使用聚合函数的列,必须出现在GROUP BY后面。

     格式:

    1. SELECT "栏位1", SUM("栏位2") FROM "表名" GROUP BY "栏位1";
    2. 例:
    3. select place_name,sum(sales) from info group by place_name order by sum(sales);

    例:

     12、HAVING------用来过滤由GROUP BY 语句返回的记录表

    • 通常与GROUP BY语句联合使用
    • HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。

    格式:

    1. select "栏位1", sum("栏位2") from "表格名" group by "栏位1" having (函数条件);
    2. 例:
    3. select place_name,sum(sales) from info group by place_name having sum(sales) > 400;

    例:

     13、别名------栏位別名,表格別名

    格式:

    1. select "表格別名"."栏位1" [AS] "栏位別名" from "表格名" [AS] "表格別名";
    2. 例:
    3. select A.place_name "name",sum(A.sales) "sum_sales" from info A group by name;

    例:

     14、子查询:连接表格

    • 子WHERE子句或HAVING子句中插入另一个SQL语句

    格式:

    1. #外查询
    2. select "栏位1" from "表格1" where "栏位2" [比较运算符]
    3. #内查询
    4. (select "栏位1" from "表格2" where "条件");
    5. #可以是符号的运算符,例如:=、>、<、>=、<= ;也可以是文字的运算符,例如 like、between、in

    例:

    一、

    1. select sum(sales) from info where place_name in (select place_name from destination where region = 'southwest');
    2. #简化版
    3. select sum(sales) from info where place_name in('chengdu','kunming');

     二、

    select sum(a.sales) from info a where a.place_name in(select place_name from destination b where b.place_name = a.place_name);
    

     15、EXISTS------用来测试内查询有没有产生任何结果

    • 类似布尔值是否为真
    • 如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。

    格式:

    1. select "栏位1" from "表格1" where exists (select * from "表格2" where "条件");
    2. 例:
    3. select sum(sales) from info where exists (select * from destination where region = 'southwest');

    连接查询

    实验表数据 

     1、inner join(等值相连)

    • 只返回两个表中联结字段相等的行

    格式:

    1. select * from11的别名 inner join 表22的别名 on1别名.栏位 =2别名.栏位; #这里的栏位名称相同
    2. 例:
    3. select * from destination a inner join info b on a.place_name = b.place_name;

    例:

     2、left join(左联接)

    格式:

    • 返回包括左表中的所有记录和右表中联结字段相等的记录
    1. select * from11的别名 left join 表22的别名 on1别名.栏位 =2别名.栏位; #这里的栏位名称相同
    2. 例:
    3. select * from destination a left join info b on a.place_name = b.place_name;

    例:

     Tip:联接方式偏向表一

    3、right join(右联接)

    • 返回包括右表中的所有记录和左表中联结字段相等的记录

    格式:

    1. select * from11的别名 right join 表22的别名 on1别名.栏位 =2别名.栏位; #这里的栏位名称相同
    2. 例:
    3. select * from destination a right join info b on a.place_name = b.place_name;

    例:

     Tip:右联接和左联接相似,不同于左联接的是右联接连接方式偏向于表二

    CREATE VIEW 视图

    • 视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
    • 临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
    • 视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。

    格式:

    1. create view "视图表名" as "select 语句";
    2. 例:
    3. create view new_info as select a.region region,a.place_name place_name,b.sales sales from destination a inner join info b on a.place_name = b.place_name;
    4. select * from new_info;
    5. drop view new_info;

    例:

    MySQL之联集、交集值、无交集值、case

    1、联集

    • 将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类

    (1)UNION

    1. union:生成结果的资料值将没有重复,且按照字段的顺序进行排序
    2. 语法:[select 语句 1] union [select 语句 2];
    3. 例:
    4. select place_name from destination union select place_name from info;

     (2)UNION ALL

    1. union all :将生成结果的资料值都列出来,无论有无重复
    2. 语法:[select 语句 1] union all [select 语句 2];
    3. 例:
    4. select place_name from destination union all select place_name from info;

     

     2、交集值

    • 取两个SQL语句结果的交集
    1. select a.place_name from destination a inner join info b on a.place_name = b.place_name;
    2. select a.place_name from destination a inner join info b using(place_name);

    • 两表没用重复的行,并且确实有交集的时候用
    select a.place_name from (select place_name from destination union all select place_name from info) a group by a.place_name having count(*) > 1;
    

    • 取两个SQL语句结果的交集,且没有重复
    1. select a.place_name from (select b.place_name from destination b inner join info c on b.place_name =c.place_name) a group by a.place_name;
    2. select distinct a.place_name from destination a inner join info b using(place_name);
    3. select distinct place_name from destination where (place_name) in (select place_name from info);
    4. select distinct a.place_name from destination a left join info b using(place_name) where b.place_name is not null;

     3、无交集值

    • 显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复
    1. select distinct place_name from destination where (place_name) not in (select place_name from info);
    2. select distinct a.place_name from destination a left join info b using(place_name) where b.place_name is null;

    两张表数据

     

     MySQL之正则表达式

    匹配模式            描述                                                                实例
    ^               匹配文本的开始字符                                ‘^bd’ 匹配以 bd 开头的字符串
    $               匹配文本的结束字符                                ‘qn$’ 匹配以 qn 结尾的字符串
    .                匹配任何单个字符                                     ‘s.t’ 匹配任何 s 和 t 之间有一个字符的字符串
    *                匹配零个或多个在它前面的字符               ‘fo*t’ 匹配 t 前面有任意个 o
    +               匹配前面的字符 1 次或多次                     ‘hom+’ 匹配以 ho 开头,后面至少一个m 的字符串
    字符串       匹配包含指定的字符串                         ‘clo’ 匹配含有 clo 的字符串
    p1|p2         匹配 p1 或 p2                                     ‘bg|fg’ 匹配 bg 或者 fg
    [...]             匹配字符集合中的任意一个字符                 ‘[abc]’ 匹配 a 或者 b 或者 c
    [^...]           匹配不在括号中的任何字符                     ‘[^ab]’ 匹配不包含 a 或者 b 的字符串
    {n}             匹配前面的字符串 n 次                                 ‘g{2}’ 匹配含有 2 个 g 的字符串
    {n,m}         匹配前面的字符串至少 n 次,至多m 次            ‘f{1,3}’ 匹配 f 最少 1 次,最多 3 次

    格式:

    1. select "栏位" from "表名" where "栏位" regexp {模式};
    2. 例:我这里使用上面实验的库
    3. use plane
    4. select * from info where place_name regexp '^b';
    5. select * from info where place_name regexp 'ei|un';

  • 相关阅读:
    Linux上使用ldapsearch命令通过AD GC查询指定用户
    Docker 必知必会1----初识
    Linux虚拟机搭建RabbitMQ集群
    前端发起请求,后端响应请求的整个过程
    【Shell编程】快速入门变量、字符串、传递参数
    教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
    Ubuntu18.04安装深度学习环境(Anaconda、显卡驱动、CUDA、Tensorflow-GPU等)
    强化学习--多维动作状态空间的设计
    每章一篇博客带你拿下吉林大学JAVAEE期末(七:会话Bean)
    重链剖分链加与极值
  • 原文地址:https://blog.csdn.net/weixin_65580338/article/details/127765513