栏位别名表格別名
语法: SELECT "表格别名". "栏位1" [AS] "栏位别名”FROM "表格名" [AS] "表格别名"; #AS可省略
SELECT A.Store_Name Store,SUM(A.Sales) "Total Sales" FROM Store_Info A GROUP BY A.Store_Name;
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL 语句
语法: SELECT "栏位1" FROM "表格1" WHERE "栏位2”[比较运算符] #外查询
(SELECT "栏位1" FROM "表格2" WHERE "条件"); #内查询
#可以是符号的运算符,例如=、>、<. >=、<= ; 也可以是文字的运算符,例如LIKE、 IN、BETWEEN
SELECT SUM(Sales) FROM Store_Info WHERE Store_Name IN
(SELECT Store_Name FROM location WHERE Region = 'West');
SELECT SUM(A.Sales) FROM Store_Info A WHERE A.Store_Name IN
(SELECT Store_Name FROM location B WHERE B.Store_Name = A.Store_Name);
用来测试内查询有没有产生任何结果,类似布尔值是否为真
#如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个SQL 语句就不会产生任何结果。
语法: SELECT "栏位1”FROM “表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
SELECT SUM(Sales) FROM Store_Info WHERE EXISTS (SELECT * FROM location WHERE Region = 'West');
只返回两个表中联结字段相等的行
SELECT * FROM location A INNER JOIN Store_Info B on A.Store_Name = B.Store_Name;
返回包括左表中的所有记录和右表中联结字段相等的记录
SELECT * FROM location A left JOIN Store_Info B on A.Store_Name = B.Store_Name;
SELECT * FROM location A, Store_Info B WHERE A.Store_Name = B.Store_Name;
返回包括右表中的所有记录和左表中联结字段相等的记录
SELECT * FROM location A right JOIN Store_Info B on A.Store_Name = B.Store_Name;
可以被当作是虚拟表或存储查询
●视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
●临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
●视图不含有数据,只存储它的定义,它的用途一.般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
语法: CREATE VIEW "视图表名" AS "SELECT 语句";
CREATE VIEW V_REGION SALES AS SELECT A.Region REGION,SUM(B.Sales) SALES FROM location A
INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name GROUP BY REGION;
SELECT * FROM V_REGION_SALES;
DROP VIEW V_REGION_SALES;
将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类
生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法:[SELECT语句1] UNION [SELECT 语句2];
select Store_Name FROM location UNION select Store_Name FROM Store_Info;
将生成结果的资料值都列出来,无论有无重复
语法: [SELECT 语句1] UNION ALL [SELECT 语句2];
select Store_Name FROM location UNION ALL select Store_Name FROM Store_Info;
取两个sQL语句结果的交集
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name;
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B USING (Store_Name);
#两表其中的一个表没用单独重复的行,而另一个表有重复行,并且要求两个表确实有交集行的时候用
SELECT A.Store_Name FROM
(SELECT store_Name FROM location UNION ALL SELECT Store_Name FROM Store_Info) A
GROUP BY A.Store_Name HAVING COUNT(*) > 1;
#取两个SQL语句结果的交集,且没有重复
SELECT A.Store_Name FROM (SELECT B.Store_Name FROM location B INNER JOIN Store_Info C ON B.Store_Name =C.store_Name) A
GROUP HY A.store_Name;
SELECT DISTINCT A.Store_Name FROM location A INNER JOIN Store_Info B USING(Store_Name);
SELECT DISTINCT Store_Name FROM location WHERE (Store_Name) IN (SELECT store_Name FROM Store_Info);
SELECT DISTINCT A.Store_Name FROM location A LEFT JOIN store_Info B USING(Store_Name) WHERE B.Store_Name IS NOT NULL;
显示第一个SQL语句的结果,且与第二个sQL语句没有交集的结果,且没有重复
SELECT DISTINCT Store_Name FROM location WHERE (Store_Name) NOT IN (SELECT Store_Name FROM Store_Info);
SELECT DISTINCT A.Store_Name FROM location A LEFT JOIN Store_Info B USING(Store_Name) WHERE B.Store_Name IS NULL;
是SQL用来做为IF-THEN-ELSE 之类逻辑的关键字
语法:
SELECT CASE ("栏位 名")
WHEN "条件1" THEN "结果1"
WHEN "条件2” THEN "结果2"
......
[ELSE "结果N"]
END
FROM "表名";
# "条件"可以是一一个数值或是公式。 ELSE 子句则并不是必须的。
SELECT Store_Name,CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'Boston' THEN Sales * 1.5
ELSE Sales * 1.5
END
"New Sales",Date
FROM Store_Info;
#"New Sales"是用于 CASE 那个栏位的栏位名。
准备一张新表
CREATE TABLE Total_Sales (Name char(10),Sales int(5));
INSERT INTO Total_Sales VALUES ('zhangsan',10);
INSERT INTO Total_Sales VALUES ('lisi',15);
INSERT INTO Total_Sales VALUES ('wangwu',20);
INSERT INTO Total_Sales VALUES ('zhaoliu',40);
INSERT INTO Total_Sales VALUES ('sunqi',50);
INSERT INTO Total_Sales VALUES ('zhouba',20);
INSERT INTO Total_Sales VALUES ('wujiu',30) ;
表格自我连结(Self Join), 然后将结果依序列出,算出每一-行之前(包含那- -行本身)有多少行数
SELECT A1.Name, A1.Sales, COUNT (A2.Sales) Rank FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name, A1.Sales ORDER BY A1.Sales DESC;
#统计Sales栏位的值是比自己本身的值小的以及Sales栏位和Name栏位都相同的数量,比如zhangsan为6+1-7
SELECT Sales Middle FROM (SELECT A1.Name, A1.Sales, COUNT(A2.Sales) Rank FROM Total_Sales A1, Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales = A2.Sales AND A1.Name >= A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC) A3
WHERE A3.Rank = (SELECT (COUNT(*) +1) DIV 2 FROM Total_Sales);
#每个派生表必须有自己的别名,所以别名A3必须要有
#DIV 是在MySQL 中算出商的方式
表格自我连结(Self Join), 然后将结果依序列出,算出每一行之前(包含那一行本身)的总合
SELECT A1.Name,A1.Sales,SUM(A2.Sales) Sum_Total FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
SELECT A1.Name,A1.Sales,A1.Sales/(SELECT SUM(Sales) FROM Total_Sales) Per_Total
FROM Total_Sales A1,Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales AND A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;
#SELECT SUM(Sales) FROM Total_Sales 这一段子查询是用来算出总合
#总合算出后,我们就能够将每一行一一除以总合来求出每一行的总合百分比
SELECT A1.Name,A1.Sales, SUM(A2.Sales)/(SELECT SUM(Sales) FROM Total_Sales) Per_Total
FROM Total_Sales A1, Total_Sales A2
WHERE A1.Sales < A2.Sales OR (A1.Sales=A2.Sales and A1.Name = A2.Name)
GROUP BY A1.Name,A1.Sales ORDER BY A1.Sales DESC;