MySQL高级语句2
一、连接查询
1、inner join(等值相连)
只返回两个表中联结字段相等的行
SELECT * FROM 表1 表1的别名 INNER JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from destination a inner join info b on a.city = b.city;
2、left join(左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
SELECT * FROM 表1 表1的别名 LEFT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from destination a left join info b on a.city = b.city;
3、right join(右联接)
返回包括右表中的所有记录和左表中联结字段相等的记录
SELECT * FROM 表1 表1的别名 RIGHT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from destination a right join info b on a.place_name = b.place_name;
二、CREATE VIEW 视图
视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
语法:CREATE VIEW "视图表名" AS "SELECT 语句";
例:
create view new_info as select a.region region,a.city city,b.price price from destination a inner join info b on a.city = b.city;
select * from new_info;
drop view new_info;
三、MySQL之联集、交集值、无交集值、case
1、联集
将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类
(1)UNION
UNION :生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法:[SELECT 语句 1] UNION [SELECT 语句 2];
例:
select city from destination union select city from info;
(2)UNION ALL
UNION ALL :将生成结果的资料值都列出来,无论有无重复
语法:[SELECT 语句 1] UNION ALL [SELECT 语句 2];
例:
select city from destination union all select city from info;
2、交集值
取两个SQL语句结果的交集
select a.city from destination a inner join info b on a.city = b.city;
select a.city from destination a inner join info b using(city);
两表没用重复的行,并且确实有交集的时候用
select a.city from (select city from destination union all select city from info) a group by a.city having count(*) > 1;
取两个SQL语句结果的交集,且没有重复
select a.city from (select b.city from destination b inner join info c on b.city =c.city) a group by a.city;
select distinct a.city from destination a inner join info b using(city);
select distinct city from destination where (city) in (select city from info);
select distinct a.city from destination a left join info b using(city) where b.city is not null;
3、无交集值
显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复
select distinct city from destination where (city) not in (select city from info);
select distinct a.city from destination a left join info b using(city) where b.city is null;
4、CASE
CASE是 SQL 用来做为 IF-THEN-ELSE 之类逻辑的关键字
语法:
SELECT CASE ("栏位名")
WHEN "条件1" THEN "结果1"
WHEN "条件2" THEN "结果2"
...
[ELSE "结果N"]
END
FROM "表名";
# "条件" 可以是一个数值或是公式。 ELSE 子句则并不是必须的。
例如:
select city, case city
when '北京' then price + 88
when '南京' then price + 88
else price
end
"new price",date
from info;
#"New Sales" 是用于 CASE 那个栏位的栏位名。
四、空值(NULL) 和 无值(’’) 的区别
无值的长度为 0,不占用空间的;而 NULL 值的长度是 NULL,是占用空间的。
IS NULL 或者 IS NOT NULL,是用来判断字段是不是为 NULL 或者不是 NULL,不能查出是不是无值的。
无值的判断使用=’‘或者<>’'来处理。<> 代表不等于。
在通过 count()指定字段统计有多少行数时,如果遇到 NULL 值会自动忽略掉,遇到无值会加入到记录中进行计算。
SELECT length(NULL), length(''), length('1');
测试
#建表
create table test(test varchar(20));
insert into test values(' ');
insert into test values('1111');
insert into test values();
insert into test values('2222');
insert into test values('');
select * from test;
例:
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;
五、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 test where test regexp '^1';
select * from test where test regexp '1|2';
六、存储过程
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 ;
4、调用存储过程
CALL 过程名;
例:
call cha_info;
5、查看存储过程
SHOW CREATE PROCEDURE [数据库.]存储过程名; #查看某个存储过程的具体信息
例:
show create procedure cha_info;
show procedure status like '%cha_info'\G
6、存储过程的参数
IN 输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
OUT 输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
INOUT 输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)
例如:
delimiter !!
create procedure test3(in incity char(20))
begin
select * from info where city = incity;
end !!
delimiter ;
call test3('北京');
7、删除存储过程
存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名称创建新的存储过程。
DROP PROCEDURE 过程名; #仅当存在时删除,如果指定的过程不存在,会产生一个错误
DROP PROCEDURE IF EXISTS 过程名; #所以可以使用if exists确定存在后删除,不存在则不执行删除。
例:
drop procedure if exists test3;
8、存储过程的控制语句
create table t (id int);
insert into t values(10);
(1)条件语句if-then-else-end if
delimiter $$
create procedure test1(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 test1(6);
call test1(4);
(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 test2;