MySQL 语句
- 1.准备表信息
- 2.MySQL高级语句
- 2.1 别名
- 2.2 子查询:连接表格
- 2.3 EXISTS
- 3.连接查询
- 3.1 inner join(等值相连)
- 3.2 left join(左联接)
- 3.3 right join(右联接)
- 4.CREATE VIEW 视图
- 5.MySQL之联集、交集值、无交集值、case
- 5.1 联集
- (1)UNION
- (2)UNION ALL
- 5.2 交集值
- 5.3 无交集值
- 5.4 CASE
- 6.排序
- 6.1 算排名
- 6.2 算中位数
- 6.3 算累积总计
- 6.4 算总合百分比
- 6.4 算累积总合百分比
1.准备表信息
2.MySQL高级语句
2.1 别名
栏位別名,表格別名
语法:SELECT “表格別名”.“栏位1” [AS] “栏位別名” FROM “表格名” [AS] “表格別名”;
例:
select A.age '年龄' from REGION A order by 年龄 asc;
2.2 子查询:连接表格
子WHERE子句或HAVING子句中插入另一个SQL语句
语法:
#外查询
SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符]
#内查询
(SELECT "栏位1" FROM "表格2" WHERE "条件");
#可以是符号的运算符,例如:=、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
例:
select A.name,age from REGION A where A.name in (select B.name from FARE B where city ='Nanjing');
2.3 EXISTS
用来测试内查询有没有产生任何结果
类似布尔值是否为真
如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
语法:SELECT “栏位1” FROM “表格1” WHERE EXISTS (SELECT * FROM “表格2” WHERE “条件”);
例:
select sum(age) from REGION where exists (select * from FARE where city ='Nanjing');
select sum(age) from REGION where exists (select * from FARE where city ='Nan');
3.连接查询
3.1 inner join(等值相连)
只返回两个表中联结字段相等的行
SELECT * FROM 表1 表1的别名 INNER JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from REGION A inner join FARE B on A.name = B.name;
3.2 left join(左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
SELECT * FROM 表1 表1的别名 LEFT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from REGION A left join FARE B on A.name = B.name;
3.3 right join(右联接)
返回包括右表中的所有记录和左表中联结字段相等的记录
SELECT * FROM 表1 表1的别名 RIGHT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select * from REGION A right join FARE B on A.name = B.name;
4.CREATE VIEW 视图
视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
语法:CREATE VIEW “视图表名” AS “SELECT 语句”;
例:
create view new_info as select A.name '姓名', A.age '年龄', B.city '城市' from REGION A inner join FARE B on A.name= B.name;
select * from new_info;
drop view new_info;
5.MySQL之联集、交集值、无交集值、case
5.1 联集
将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类
(1)UNION
UNION :生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法:[SELECT 语句 1] UNION [SELECT 语句 2];
例:
select name from REGION union select name from FARE;
(2)UNION ALL
UNION ALL :将生成结果的资料值都列出来,无论有无重复
语法:[SELECT 语句 1] UNION ALL [SELECT 语句 2];
例:
select name from REGION union all select name from FARE;
5.2 交集值
取两个SQL语句结果的交集
select A.name from REGION A inner join FARE B on A.name = B.name;
两表没用重复的行,并且确实有交集的时候用
select name from (select name from REGION union all select name from FARE) A group by A.name me having count(*) > 1;
取两个SQL语句结果的交集,且没有重复
select name from (select A.name from REGION A inner join FARE B on A.name = B.name) C group by C.name;
5.3 无交集值
显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复
select distinct name from FARE where (name) not in (select name from REGION);
5.4 CASE
CASE是 SQL 用来做为 IF-THEN-ELSE 之类逻辑的关键字
语法:
SELECT CASE (“栏位名”)
WHEN “条件1” THEN “结果1”
WHEN “条件2” THEN “结果2”
…
[ELSE “结果N”]
END
FROM “表名”;
** “条件” 可以是一个数值或是公式。 ELSE 子句则并不是必须的。**
6.排序
在排序前先准备下用来实验的表格
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);
insert into class values ('hh8',98);
select * from class;
6.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
6.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 中算出商的方式
6.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;
6.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 这一段子查询是用来算出总合
#总合算出后,我们就能够将每一行一一除以总合来求出每一行的总合百分比
6.4 算累积总合百分比
用累积总计 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;
取小数点后几位数
select A1.name,A1.scores,sum(A2.scores),truncate((sum(A2.scores)/(select sum(scores) from class)*100),0) ||'%' per_sum from class A1,class A2 where A1.scores < A2.scores or (A1.scores = A2.scorees and A1.name=A2.name) group by A1.name order by A1.scores desc;