目录
1. 创建表插入数据
2. 连接类型
2.1 内连接
2.2 外连接
2.2.1 左外连接
2.2.2 右外连接
2.3 交叉连接
2.4 子查询&公用表达式
2.5 UNION连接
3. SQL常用函数
3.1 CASE WHEN函数
3.2 COALESCE函数
3.3 NULLIF函数
3.4 LEAST 和 GREATEST函数
3.5 DISTINCT 函数
DROP TABLE IF EXISTS 经销商;
create table 经销商
(
dealership_id INT PRIMARY KEY,
street_adress TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
latitude DOUBLE PRECISION,
longitude DOUBLE PRECISION,
date_opened TIMESTAMP,
date_closed TIMESTAMP
)engine=innodb;
DROP TABLE IF EXISTS 销售人员;
create table 销售人员
(
salesperson_id INT PRIMARY KEY,
dealership_id INT,
title TEXT,
first_name TEXT,
last_name TEXT,
suffix TEXT,
username TEXT,
gender TEXT,
hire_date TIMESTAMP,
termination TIMESTAMP
)engine=innodb;
DROP TABLE IF EXISTS 顾客;
create table 顾客
(
customer_id INT PRIMARY KEY,
street_adress TEXT,
city TEXT,
state TEXT,
postal_code TEXT
)engine=innodb;
INSERT INTO 经销商 VALUES(1,'library','GuangZhou','LF','54321',30.00,-180.88,'2023-07-10 00:00:00','2023-07-10 23:00:00');
INSERT INTO 经销商 VALUES(2,'office','XiaMeng','LF','54331',40.00,-170.88,'2023-07-10 00:00:00','2023-07-10 23:00:00');
INSERT INTO 经销商 VALUES(3,'langbao','HuangZhou','XS','54421',50.00,-160.88,'2023-07-11 00:00:00','2023-07-11 23:00:00');
INSERT INTO 经销商 VALUES(4,'yangcun','WuHan','XS','54321',60.00,-150.88,'2023-07-12 00:00:00','2023-07-12 23:00:00');
INSERT INTO 经销商 VALUES(5,'forest','ShangHai','JFJ','64321',70.00,-140.88,'2023-07-13 00:00:00','2023-07-13 23:00:00');
INSERT INTO 经销商 VALUES(6,'huayuan','BeiJing','QZ','65432',80.00,-130.88,'2023-07-14 00:00:00','2023-07-14 23:00:00');
INSERT INTO 经销商 VALUES(7,'chengbao','GuangZhou','YY','43210',90.00,-120.88,'2023-07-15 00:00:00','2023-07-15 23:00:00');
INSERT INTO 经销商 VALUES(8,'lantian','GuangZhou','DD','76543',100.00,-110.88,'2023-07-16 00:00:00','2023-07-16 23:00:00');
INSERT INTO 经销商 VALUES(9,'baiyun','XiaMeng','LF','54311',110.00,-100.88,'2023-07-16 00:00:00','2023-07-16 23:00:00');
INSERT INTO 经销商 VALUES(10,'caoyuan','XiaMeng','LF','43210',120.00,-90.88,'2023-07-17 00:00:00','2023-07-17 23:00:00');
INSERT INTO 销售人员 VALUES(2,0,NULL,'Lin','QingFeng','A','admin','female','2023-08-15 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(4,1,NULL,'Li','LingXiao','A','admin1','male','2023-08-17 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(6,2,NULL,'Bai','JianJian','B','admin2','female','2023-08-19 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(8,3,NULL,'Ye','DanDan','B','admin3','male','2023-08-20 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(10,4,NULL,'Qin','KaiKai','A','admin4','female','2023-08-25 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(11,5,NULL,'Gu','ZhanGe','A','admin5','female','2023-08-28 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(12,6,NULL,'Deng','XiaoWu','C','admin4','male','2023-08-30 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(13,6,NULL,'Chen','HuiLai','A','admin3','male','2023-08-31 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(14,8,NULL,'Xiao','FeiLun','C','admin2','female','2023-09-03 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(20,9,NULL,'Guo','XiaoYang','B','admin','male','2023-09-05 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(22,10,NULL,'Guo','XiaoYang','B','admin','male','2023-09-05 00:00:00',NULL);
INSERT INTO 销售人员 VALUES(23,12,NULL,'Qiu','XiaoYan','B','admin','male','2023-09-05 00:00:00',NULL);
INSERT INTO 顾客 VALUES(1,'huayuan','GuangZhou','AB','44321');
INSERT INTO 顾客 VALUES(3,'lantian','XiaMeng','AA','44021');
INSERT INTO 顾客 VALUES(4,'baiyun','GuangZhou','AR','49321');
INSERT INTO 顾客 VALUES(5,'feiji','XiaMeng','AF','44381');
INSERT INTO 顾客 VALUES(6,'xiaoniao','GuangZhou','AV','84321');
INSERT INTO 顾客 VALUES(7,'jiayuan','ShangHai','AB','44521');
INSERT INTO 顾客 VALUES(8,'guojia','GuangZhou','AQ','44321');
INSERT INTO 顾客 VALUES(9,'chengshi','GuangZhou','AO','43321');
INSERT INTO 顾客 VALUES(10,'nongcun','XiaMeng','AP','44371');
INSERT INTO 顾客 VALUES(11,'hudie','HangZhou','AY','44329');
INSERT INTO 顾客 VALUES(12,'tongnian','GuangZhou','AE','40321');
INSERT INTO 顾客 VALUES(14,'tongnian',NULL,'AE','40321');
INSERT INTO 顾客 VALUES(15,'tongnian',NULL,'AE','40321');
INSERT INTO 顾客 VALUES(16,'tongnian',NULL,'AE','40321');
SELECT * FROM 经销商 INNER JOIN 销售人员 ON 经销商.dealership_id=销售人员.dealership_id;
从查询结果可以看出,该表是将“经销商”表连接到“销售人员”表 ,且“经销商”表在左侧,“销售人员”表在右侧。
如果只想检索其中一个表,如“经销商”表,可使用下列语句:
SELECT 经销商.* FROM 经销商 INNER JOIN 销售人员 ON 经销商.dealership_id=销售人员.dealership_id where city='XiaMeng';
#或
SELECT J.* FROM 经销商 J INNER JOIN 销售人员 X ON J.dealership_id=X.dealership_id where city='XiaMeng';
#或
SELECT J.* FROM 经销商 AS J INNER JOIN 销售人员 AS X ON J.dealership_id=X.dealership_id where city='XiaMeng';
内连接只会返回来自两个表的行,并且只有当两个行都满足连接要求时才会返回,否则不会返回任一表中的任何行。
如果希望从其中一个表中返回所有行,而不管是否满足连接要求。在这种情况下,如果连接要求未被满足,则第二个表的行将返回NULL,这样的连接称为外连接。在外连接操作后,其中至少有一个表出现在结果的每一行中。
外连接可以分为3类:左外连接,右外连接、全外连接(mysql不直接支持全外连接full outer join)
在左外连接中(left outer join),左表将返回每一行,如果右表不满足join条件,则返回NULL行。
SELECT * FROM 销售人员 X LEFT OUTER JOIN 经销商 J ON J.dealership_id=X.dealership_id;
SELECT * FROM 经销商 J LEFT OUTER JOIN 销售人员 X ON J.dealership_id=X.dealership_id;
右外连接与左外连接具有对称性,在右外连接中,右表将显示每一行,如果join条件未被满足,则左表将包含NULL值。
SELECT * FROM 经销商 J RIGHT OUTER JOIN 销售人员 X ON J.dealership_id=X.dealership_id;
SELECT * FROM 销售人员 X RIGHT OUTER JOIN 经销商 J ON J.dealership_id=X.dealership_id;
交叉连接也称笛卡尔积,它将返回左表和右表的所有可能的行组合。
SELECT J.dealership_id,J.city,X.dealership_id,X.suffix FROM 经销商 J CROSS JOIN 销售人员 X;
可以看到查询结果有120行(10*12),交叉连接不需要连接条件,也被认为是一个没有连接条件的外连接。
子查询:使用select查询生成的表,而不是引用数据库中的现有表
SELECT * FROM 销售人员 INNER JOIN (SELECT * FROM 经销商 WHERE city='XiaMeng') d ON d.dealership_id=销售人员.dealership_id;
公用表达式:在某种意义上只是子查询的不同版本。公用表达式使用WITH子句建立临时表。
WITH d AS (SELECT * FROM 经销商 WHERE city='XiaMeng') SELECT * FROM 销售人员 INNER JOIN d ON d.dealership_id=销售人员.dealership_id;
UNION可以将多个查询垂直放在一起,即保持相同的列但添加多行。
SELECT street_adress,city,state,postal_code FROM 顾客 WHERE state IS NOT NULL
UNION
SELECT street_adress,city,state,postal_code FROM 经销商 WHERE state IS NOT NULL;
UNION使用注意事项:
1)UNION要求子查询具有相同名称的列和相同的列数据类型。如果没有,则查询结果不会运行。
2)在默认情况下,UNION会删除输出结果中的所有重复行。因此想要保留重复的行,可使用UNION ALL关键字。
CASE WHEN是一个允许查询将列中的各种值映射到其它值的函数。CASE WHEN语句的一般格式如下:
CASE
WHEN condition1 THEN value1
WHEN condition2 THEN value2
……
WHEN conditionX THEN valueX
ELSE else value
END;
SELECT *,
CASE
WHEN city='GuangZhou' THEN 'GuangDong'
WHEN city='XiaMeng' THEN 'FuJian'
WHEN city='HangZhou' THEN 'ZheJiang'
ELSE 'Other'
END
AS province
FROM 顾客;
COALESEC函数是用标准值替换NULL值。
SELECT customer_id,city FROM 顾客;
SELECT customer_id, COALESCE(city,'aabbcc') AS city FROM 顾客;
NULLIF函数是COALESEC的反面,将特定的值替换为NULL。
SELECT customer_id,city FROM 顾客;
SELECT customer_id,NULLIF(city,'ShangHai') AS city FROM 顾客;
LEAST和GREATEST这两个函数可接受任意数量的值并分别返回最小或最大的值。如LEAST(600,900)返回600;GREATEST(600,900)返回900,它们的参数既可以是文字值也可以是数值。
SELECT dealership_id,city,postal_code,LEAST (60000,postal_code) as postal_code_new FROM 经销商;
SELECT dealership_id,city,postal_code,GREATEST (55000,postal_code) as postal_code_new FROM 经销商;
DISTINCT用来去除某一列中的重复值。
SELECT DISTINCT city FROM 顾客;