Mysql高阶语句(二)
- 1、别名
- 2、子查询
- 3、EXISTS
- 4、连接查询
- 5、CREATE VIEW 视图
- 6、UNION 联集
- 7、交集值
- 8、无交集值
- 9、CASE
- 10、算排名
- 12、算累积总计
- 13、算总合百分比
- 14、算累计总合百分比
- 15、空值(null)和无值(’’)的区别
- 16、正则表达式(与Shell部分一样)
1、别名
字段别名、表格别名
语法:SELECT “表格別名”.“字段1” [AS] “字段1別名” FROM “表格名” [AS] “表格別名”;
select RE.region AS reg, count(site) from REGION AS RE group by reg;
select FA.site AS si,sum(money),count(money),date AS da from FARE AS FA group by si;
2、子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL 语句
语法:
SELECT 字段1 FROM 表格1 WHERE 字段2 [比较运算符] #外查询
(SELECT 字段1 FROM 表格2 WHERE 条件) #内查询
可以是符号的运算符
例:=、>、<、>=、<=
也可以是文字的运算符
例:LIKE、IN、BETWEEN
select A.site,region from REGION AS A where A.site in
(select B.site from FARE AS B where money<2000);
select B.site,money,date from FARE AS B where site in
(select A.site from REGION AS A where region = 'north');
3、EXISTS
用来测试内查询有没有产生任何结果,类似布尔值是否为真
如果有的话,系统就会执行外查询中的SQL语句,若是没有,那整个SQL语句就不会产生任何结果。
语法:
SELECT 字段1 FROM 表1 WHERE EXISTS (SELECT * FROM 表2 WHERE 条件);
例:
select region from REGION where exists (select * from FARE where money = '700');
4、连接查询
MysQL的连接查询,通常都是将来自两个或多个表的行结合起来,基于这些表之间的共同字段,进行数据的拼接。
首先,要确定一个主表作为结果集,然后将其他表的行有选择性的连接到选定的主表结果集上。
使用较多的连接查询包括:内连接、左连接和右连接(inner join、left join、right join)
inner join(等值相连)
只返回两个表中联接字段相等的行
语法:SELECT 字段 FROM 表1 INNER JOIN 表2 ON 表1.字段 = 表2.字段;
left join(左联接)
返回包括左表中所有记录和右表中联接字段相等的记录
语法:SELECT 字段 FROM 表1 LEFT JOIN 表2 ON 表1.字段 = 表2.字段;
right join(右联接)
返回包括右表中的所有记录和左表中联接字段相等的记录
语法:SELECT 字段 FROM 表1 RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;
例:
select * from king A right join clothe B on A.hero_name = B.hero_name;
select * from king A left join clothe B on A.hero_name = B.hero_name;
select * from king A inner join clothe B on A.hero_name = B.hero_name;
select * from king A inner join clothe B using(hero_name);
5、CREATE VIEW 视图
-
视图:可被当作虚拟表或存储查询。
-
视图跟表格不同的是,表格中有实际存储资料,而视图是建立在表格之上的一个架构,它本身并不实际存储资料。
-
临时表在用户退出或者同数据库的连接断开后就自动消失了,而视图不会。
-
视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表格进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦,用视图将几个表格连接起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
例:
语法:CREATE VIEW 视图表名 AS SELECT语句;
select A.region,sum(B.money) from region as A inner join fare AS B on A.site = B.site group by region;
create view C as select A.region,sum(B.money) from region as A inner join fare AS B on A.site = B.site group by region;
show tables;
select * from C;
删除视图表
6、UNION 联集
- 将两个SQL语句的结果合并起来,两个SQL语句产生的字段需要是同样的资料种类
union:生成结果的资料值将没有重复,且按照字段的顺序进行排序。
语法:select 语句1 union select 语句2;
union all:将生成结果的资料值都列出来,无论有无重复
语法:select 语句1 union all select 语句2;
例:
select region from region union select money from fare;
select site from region union select site from fare;
select site from region union all select site from fare;
7、交集值
- 取两个SQL语句结果的交集
例:
select * from fare;
insert into fare values ('xingjiang',2200,'2021-02-05');
select * from fare;
select A.site from fare as A inner join region as B on A.site = B.site;
select A.site from fare as A inner join region as B using(site);
select site from (select site from fare union all select site from region) A group by A.site having count(*) > 1;
select site from (select A.site from fare A inner join region B on A.site = B.site) C group by C.site;
select site from fare where site in (select site from region);
8、无交集值
- 显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,还不能重复
select distinct site from fare where (site) not in (select site from region);
例:
select A.*,B.* from fare A left join region B using(site);
select site from fare A left join region B using(site) where B.site is null;
select distinct site from fare A left join region B using(site) where B.site is null;
9、CASE
- 是SQL用来作为IF-THEN-ELSE之类逻辑的关键字
语法:
SELECT CASE (字段名)
WHEN 条件1 THEN 结果1
WHEN 条件2 THEN 结果2
……
ELSE 结果N
END
FROM 表名
#条件可以是一个数值或是公式。ELSE子句不是必须的。
例:
select * from fare;
select case site
when 'changsha' then money + 500
when 'tianjin' then money - 500
else money * 2
end
A,site
from fare;
10、算排名
- 表格自我连接(self join),然后将结果依序列出,算出每一行之前(包括那一行本身)有多少行数
例:
create table SCORE (name char(10),score int(4));
insert into SCORE values('zhangsan',95);
insert into SCORE values('lisi',96);
insert into SCORE values('wangwu',75);
insert into SCORE values('zhaoliu',80);
insert into SCORE values('qianqi',90);
select * from SCORE;
select A1.name,A1.score,count(A2.score) rank from SCORE A1,SCORE A2 where A1.score < A2.score OR (A1.score=A2.score and A1.name=A2.name) group by A1.name order by A1.score desc;
解释:
- 当A1的score字段值小于A2的score字段值、或者两表score字段值相等并且name字段值相等时,从A1和A2表中 查询A1的name字段值、A1的score字段值、和A2的score字段的非空值 rank是别名 ,并为A1的name字段分组,A1的score字段降序排序
A1.score为75时,A2.score可为75、80、90、95、96 则 count(A2.score)为5
A1.score为80时,A2.score可为80、90、95、96 则 count(A2.score)为4
A1.score为90时,A2.score可为90、95、96 则 count(A2.score)为3
A1.score为95时,A2.score可为95、96 则 count(A2.score)为2
A1.score为96时,A2.score可为96 则 count(A2.score)为1
这样再显示A1的name字段分组,A1的score字段降序排序 ,rank字段
则为下表所示
+----------+-------+------+
| name | score | rank |
+----------+-------+------+
| lisi | 96 | 1 |
| zhangsan | 95 | 2 |
| qianqi | 90 | 3 |
| zhaoliu | 80 | 4 |
| wangwu | 75 | 5 |
+----------+-------+------+
- 统计Sales栏位的值是比自己本身的值小得以及Sales栏位得Name栏位都相同的数量,比如zhangsan为6+1=7
11、算中位数
例:
select * from (select A1.name,A1.score,count(A2.score) rank from SCORE A1,SCORE A2 where A1.score < A2.score OR (A1.score=A2.score and A1.name=A2.name) group by A1.name order by A1.score desc) A3 where A3.rank = (select (count(*)+1) DIV 2 from SCORE);
select score mid from (select A1.name,A1.score,count(A2.score) rank from SCORE A1,SCORE A2 where A1.score < A2.score OR (A1.score=A2.score and A1.name=A2.name) group by A1.name order by A1.score desc) A3 where A3.rank = (select (count(*)+1) DIV 2 from SCORE);
- 每个派生表必须有自己的别名,所以别名A3必须要有
- DIV是在MySQL中算出商的方式
12、算累积总计
表格自我连结(Self Join),然后将结果依序列出,算出每一行之前(包含哪一行本身)的总合
例:
select A1.*,sum(A2.score) sum_socore from SCORE A1,SCORE A2 where A1.score < A2.score or(A1.score=A2.score and A1.name=A2.name) group by A1.name order by A1.score desc;
13、算总合百分比
例:
select A1.*,A1.score/(select sum(score) from SCORE) z_sum from SCORE A1,SCORE A2 where A1.score < A2.score or (A1.score=A2.score and A1.name=A2.name) group by A1.name;
1
select sum(socre) from SCORE 是为了算出字段值总合,然后每一行一一除以总合,算出每行的总合百分比。
14、算累计总合百分比
例:
select A1.name,A1.score,sum(A2.score),sum(A2.score)/(select sum(score) from SCORE) Z from SCORE A1,SCORE A2 where A1.score < A2.score or (A1.score=A2.score and A1.name=A2.name) group by A1.name order by A1.score desc;
select A1.name,A1.score,sum(A2.score),TRUNCATE(sum(A2.score)/(select sum(score) from SCORE),2) ||'%' Z from SCORE A1,SCORE A2 where A1.score < A2.score or (A1.score=A2.score and A11.name=A2.name) group by A1.name order by A1.score desc;
15、空值(null)和无值(’’)的区别
- 1、无值的长度为0,不占用空间;而空值null 的长度是null,是占用空间的
- 2、IS NULL或者IS NOT NULL,是用来判断字段是不是NULL或者不是NULL,是不能查出是不是无值的
- 3、无值的判断使用=’‘或者<>’'来处理。<>代表不等于
- 4、在通过count()指定字段统计又多少行数时,如果遇到NULL值会自动忽略掉,遇到空值会自动加入记录中进行计算
例:
create table SITE(site varchar(20));
insert into SITE values('nanjing');
insert into SITE values('beijing');
insert into SITE values('');
insert into SITE values('taijin');
insert into SITE values();
insert into SITE values('');
select * from SITE;
select length(site) from SITE;
select * from SITE where site is NULL;
select * from SITE where site is not NULL;
select * from SITE where site ='';
select * from SITE where site <> '';
16、正则表达式(与Shell部分一样)
匹配模式 | 描述 | 实例 |
---|---|---|
^ | 匹配文本的开始字符 | ‘^bd’ 匹配以 bd 开头的字符串 |
$ | 匹配文本的结束字符 | ‘qn$’ 匹配以 qn 结尾的字符串 |
. | 匹配任何单个字符 | ‘s.t’ s 和 t 之间匹配任何一个字符串 |
* | 匹配零个或多个在它前面的字符 | ‘fo*t’ 匹配 t 前面有任意个 o |
+ | 匹配前面的字符 1 次或多次 | ‘hom+’ 匹配以 ho 开头,后面至少一个m 的字符串 |
[…] | 匹配字符集合中的任意一个字符 | ‘[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 匹配模式
例:
select * from FARE where site regexp '^[n]';
select * from FARE where site regexp '[n]';
select * from FARE where site regexp 'jin|jing';