0
点赞
收藏
分享

微信扫一扫

数据库——MySQL高级SQL语句(二)

@toc

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

1、联集

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

    (1)UNION

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

    image.png

    (2)UNION ALL

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

    image.png

    2、交集值

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

select a.place_name from destination a inner join info b using(place_name);

![image.png](https://s2.51cto.com/images/20220215/1644936669408207.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 两表没用重复的行,并且确实有交集的时候用

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;

![image.png](https://s2.51cto.com/images/20220216/1644991157212114.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

- 取两个SQL语句结果的交集,且没有重复

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;

select distinct a.place_name from destination a inner join info b using(place_name);

select distinct place_name from destination where (place_name) in (select place_name from info);

select distinct a.place_name from destination a left join info b using(place_name) where b.place_name is not null;

![image.png](https://s2.51cto.com/images/20220216/1644991226398820.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 3、无交集值
- 显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复

select distinct place_name from destination where (place_name) not in (select place_name from info);

select distinct a.place_name from destination a left join info b using(place_name) where b.place_name is null;

![image.png](https://s2.51cto.com/images/20220216/1644991494313969.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 4、CASE
- CASE是 SQL 用来做为 IF-THEN-ELSE 之类逻辑的关键字

语法:
SELECT CASE ("栏位名")
WHEN "条件1" THEN "结果1"
WHEN "条件2" THEN "结果2"
...
[ELSE "结果N"]
END
FROM "表名";

"条件" 可以是一个数值或是公式。 ELSE 子句则并不是必须的。

例如:
select place_name, case place_name
when 'beijing' then sales - 50
when 'kunming' then sales - 20
else sales
end
"new sales",date
from info;
#"New Sales" 是用于 CASE 那个栏位的栏位名。

![image.png](https://s2.51cto.com/images/20220216/1644991741722138.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
## 二、排序
在排序前先准备下用来实验的表格

create database school;
use school;
create table class(name varchar(20),scores int(5));
insert into class values ('aa1',80);
insert into class values ('bb2',100);
insert into class values ('cc3',78);
insert into class values ('dd4',94);
insert into class values ('ee5',66);
insert into class values ('ff6',53);
insert into class values ('gg7',77);
select * from class;

![image.png](https://s2.51cto.com/images/20220216/1644991908657667.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 1、算排名
- 表格自我连结 (Self Join),然后将结果依序列出,算出每一行之前 (包含那一行本身) 有多少行数

select a1.name, a1.scores,count(a2.scores) rank from class a1,class a2 where a1.scores < a2.scores or (a1.scores = a2.scores and a1.name = a2.name) group by a1.name,a1.scores order by a1.scores desc;
#统计scores栏位的值是比自己本身的值小的以及scores栏位和name栏位都相同的数量,比如ff6为6+1=7

![image.png](https://s2.51cto.com/images/20220216/1644993720330275.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 2、算中位数

select scores middle from (select a1.name, a1.scores,count(a2.scores) rank from class a1,class a2 where a1.scores < a2.scores or (a1.scores = a2.scores and a1.name <= a2.name) group by a1.name,a1.scores order by a1.scores desc) a3 where a3.rank = (select (count(*)+1) div 2 from class);

#每个派生表必须有自己的别名,所以别名 A3 必须要有
#DIV 是在 MySQL 中算出商的方式

![image.png](https://s2.51cto.com/images/20220216/1644994194421732.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 3、算累积总计
- 表格自我连结 (Self Join),然后将结果依序列出,算出每一行之前 (包含那一行本身) 的总合

select A1.*,sum(A2.scores) sum_socores from class A1,class A2 where A1.scores < A2.scores or(A1.scores = A2.scores and A1.name = A2.name) group by A1.name order by A1.scores desc;

![image.png](https://s2.51cto.com/images/20220216/1644994462197510.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 4、算总合百分比

select A1.*,A1.scores/(select sum(scores) from class) per_sum from class A1,class A2 where A1.scores < A2.scores or (A1.scores = A2.scores and A1.name=A2.name) group by A1.name order by A1.scores desc;

#SELECT SUM(Sales) FROM Total_Sales 这一段子查询是用来算出总合
#总合算出后,我们就能够将每一行一一除以总合来求出每一行的总合百分比

![image.png](https://s2.51cto.com/images/20220216/1644994582356613.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 5、算累积总合百分比
- 用累积总计 SUM(a2.Sales) 除以总合来求出每一行的累积总合百分比

select A1.name,A1.scores,sum(A2.scores),sum(A2.scores)/(select sum(scores) from class) per_sum from class A1,class A2 where A1.scores < A2.scores or (A1.scores = A2.scores and A1.name=A2.name) group by A1.name order by A1.scores desc;

![image.png](https://s2.51cto.com/images/20220216/1644994639957641.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 取小数点后几位数

select A1.name,A1.scores,sum(A2.scores),truncate(sum(A2.scores)/(select sum(scores) from class),2) ||'%' per_sum from class A1,class A2 where A1.scores < A2.scores or (A1.scores = A2.scores and A1.name=A2.name) group by A1.name order by A1.scores desc;

![image.png](https://s2.51cto.com/images/20220216/1644994680779907.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
**SQL语句有点长,也可以采用视图的方法**

### 三、空值(NULL) 和 无值('') 的区别
- 无值的长度为 0,不占用空间的;而 NULL 值的长度是 NULL,是占用空间的。
- IS NULL 或者 IS NOT NULL,是用来判断字段是不是为 NULL 或者不是 NULL,不能查出是不是无值的。
- 无值的判断使用=’‘或者<>’'来处理。<> 代表不等于。
- 在通过 count()指定字段统计有多少行数时,如果遇到 NULL 值会自动忽略掉,遇到无值会加入到记录中进行计算。

SELECT length(NULL), length(''), length('1');

![image.png](https://s2.51cto.com/images/20220216/1644994828623272.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

- 测试

#建表
create table test(test varchar(20));
insert into test values(' ');
insert into test values('123123');
insert into test values();
insert into test values('1111');
insert into test values('');
select * from test;

![image.png](https://s2.51cto.com/images/20220216/1644994932314118.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

例:
select from test where test is null;
select
from test where test is not null;
select from test where test = '';
select
from test where test <> '';
select count(test) from test;

![image.png](https://s2.51cto.com/images/20220216/1644995133328196.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 四、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 次
    语法格式:

    语法:SELECT "栏位" FROM "表名" WHERE "栏位" REGEXP {模式};
    例:我这里使用上面实验的库
    use plane
    select from info where place_name regexp '^b';
    select
    from info where place_name regexp 'ei|un';

    
    ![image.png](https://s2.51cto.com/images/20220216/1644995596224611.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

五、存储过程

1、存储过程的概念

  • 存储过程是一组为了完成特定功能的SQL语句集合。
  • 存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来,这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时,只需要调用它即可。存储过程在执行上比传统SQL速度更快、执行效率更高。

    2、存储过程的优点

  • 执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
  • SQL语句加上控制语句的集合,灵活性高
  • 在服务器端存储,客户端调用时,降低网络负载
  • 可多次重复被调用,可随时修改,不影响客户端调用
  • 可完成所有的数据库操作,也可控制数据库的信息访问权限

    3、创建存储过程

    
    格式:
    DELIMITER $$                            #将语句的结束符号从分号;临时改为两个$$(可以是自定义)
    CREATE PROCEDURE xxx()                  #创建存储过程,过程名为xxx,不带参数
    BEGIN                                  #过程体以关键字 BEGIN 开始
    sql语句;                             #过程体语句
    END $$                                 #过程体以关键字 END 结束
    DELIMITER ;                             #将语句的结束符号恢复为分号

例:
delimiter $$
create procedure cha_info()
begin
select * from info;
end $$
delimiter ;

![image.png](https://s2.51cto.com/images/20220216/1644995953344953.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 4、调用存储过程

CALL 过程名;
例:
call cha_info;

![image.png](https://s2.51cto.com/images/20220216/1644996000530879.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 5、查看存储过程

SHOW CREATE PROCEDURE [数据库.]存储过程名; #查看某个存储过程的具体信息

例:
show create procedure cha_info;
show procedure status like '%cha_info'\G

![image.png](https://s2.51cto.com/images/20220216/1644996061141298.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 6、存储过程的参数
- IN 输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
- OUT 输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
- INOUT 输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)
例如:

delimiter !!
create procedure test1(in inname char(20))
begin
select * from info where place_name = inname;
end !!
delimiter ;

call test1('chengdu');

![image.png](https://s2.51cto.com/images/20220216/1644996137432915.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 7、删除存储过程
存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名称创建新的存储过程。

DROP PROCEDURE 过程名; #仅当存在时删除,如果指定的过程不存在,会产生一个错误
DROP PROCEDURE IF EXISTS 过程名; #所以可以使用if exists确定存在后删除,不存在则不执行删除。
例:
drop procedure if exists test1;

![image.png](https://s2.51cto.com/images/20220216/1644996210723513.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 8、存储过程的控制语句

create table t (id int);
insert into t values(10);

![image.png](https://s2.51cto.com/images/20220216/1644996239445659.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### (1)条件语句if-then-else-end if

delimiter $$
create procedure test2(in shu int)
begin
declare var int;
set var=shu*2;
if var>=10 then
update t set id=id+1;
else
update t set id=id-1;
end if;
end $$

delimiter ;

call test2(6);

![image.png](https://s2.51cto.com/images/20220216/1644996271102397.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![image.png](https://s2.51cto.com/images/20220216/1644996310463185.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### (2)循环语句while ···· end while

delimiter $$
create procedure test3()
begin
declare var int;
set var=0;
while var<6 do
insert into t values(var);
set var=var+1;
end while;
end $$

delimiter ;

call test3;


![image.png](https://s2.51cto.com/images/20220216/1644996344397059.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![image.png](https://s2.51cto.com/images/20220216/1644996368538141.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
举报

相关推荐

0 条评论