多表连接:顾名思义,就是通过各个表之间共同列的关联性来查询数据。
多表连接的查询方式又分为以下几种:内连接,外连接和交叉连接。外连接又分为:左外连接,右外连接和全外连接。
以下例子所使用的表:
SQL> select * from stu;
ID NAME CLASS
---------- -------------------- ----------
1001 H 2
1002 S 2
1003 Q 1
1004 R 3
1010 X 3
SQL> select * from score;
ID STU_SCORE
---------- ----------
1001 90
1002 87
1003 92
1004 79
1008 65
一、内连接(INNER JOIN)
内连接(INNER JOIN)使用比较操作符进行表间某些列数据的比较操作,并列出这些表中与连接条件相匹配的数据行。简写为join
select 表1.字段名1,表2.字段名2
from 表1 join 表2
on 表1.字段名=表2.字段名;
SQL> select stu.name,score.stu_score from stu join score on stu.id=score.id;
NAME STU_SCORE
-------------------- ----------
H 90
S 87
Q 92
R 79
二、外连接
1、左外连接(LEFT JOIN)
左外连接的结果集包括LEFT JOIN子句中指定的左表的所有行,不仅仅是连接列所匹配的行。如果左表中的某行在右表中没有匹配行,则在相关的连接结果集中右表的所有选择列置为空。
select 表1.字段名1,表2.字段名2
from 表1 left join 表2
on 表1.字段名=表2.字段名;
SQL> select stu.name,score.stu_score from stu left join score on stu.id=score.id;
NAME STU_SCORE
-------------------- ----------
H 90
S 87
Q 92
R 79
X
2、右外连接(RIGHT JOIN)
右外连接与左外连接相反。返回右表的所有行,如果左表中没有匹配行,则将左表的所有选择列置为空。
select 表1.字段名1,表2.字段名2
from 表1 right join 表2
on 表1.字段名=表2.字段名;
SQL> select stu.name,score.stu_score from stu right join score on stu.id=score.id;
NAME STU_SCORE
-------------------- ----------
H 90
S 87
Q 92
R 79
65
3、全外连接(FULL JOIN)
返回左表和右表的全部行,如果右表中没有匹配行,则将左表的所有选择列置为空。如果坐标中没有匹配行,则将右表的所有选择列置为空。
select 表1.字段名1,表2.字段名2
from 表1 full join 表2
on 表1.字段名=表2.字段名;
SQL> select stu.name,score.stu_score from stu full join score on stu.id=score.id;
NAME STU_SCORE
-------------------- ----------
H 90
S 87
Q 92
R 79
65
X
三、交叉连接(CROSS JOIN)
交叉连接不带WHERE 子句,它返回被连接的两个表所有数据行的笛卡尔积,返回到结果集合中的数
据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
SQL> select stu.name,score.stu_score from stu full join score on stu.id=score.id;
四、set操作符
set操作符的使用规则:
①、两个集合查询的列数要相同
②、查询的列的数据类型要相同,只要属于相同的类型组就可以
③、查询的次序要相同
④、可以使用括号改变优先级
⑤、order by 值可以出现在查询子句最后
⑥、最终结果使用第一个查询的列名
1、union all 连接没有关系的结果集合(唯一一个不去重的)
SQL> select count(*) from stu where class=1
2 union all
3 select count(*) from stu where class=2
4 union all
5 select count(*) from stu where class=3;
COUNT(*)
----------
1
2
2
2、union 在union all的基础上排序,另外结果默认是以首列升序排序
SQL> select count(*) from stu where class=1
2 union
3 select count(*) from stu where class=2
4 union
5 select count(*) from stu where class=3;
COUNT(*)
----------
1
2
3、intersect 求交集 ,将两个集合中相同的部分返回
SQL> select id from stu
2 intersect
3 select id from score;
ID
----------
1001
1002
1003
1004
4、minus 求差集,减去A中包含于B的部分
SQL> select id from stu
2 minus
3 select id from score;
ID
----------
1010
五、子查询
子查询:把子查询的结果作为主查询的条件
子查询的结果可以写在select 、from 、 where 中
1、单行子查询(子查询结果仅返回一行)
单行操作符:= > < >= <= <>
SQL> select id,stu_score from score where stu_score>(select STU_score from score where id=1002);
ID STU_SCORE
---------- ----------
1001 90
1003 92
1、多行子查询
多行操作符: IN ANY ALL
①、IN 即:stu_score=value1 or stu_score=value2 or stu_score=value3
SQL> select id,stu_score from score where stu_score in (select STU_SCORE from score where stu_score>80);
ID STU_SCORE
---------- ----------
1001 90
1002 87
1003 92
not in 即:stu_score<>value1 and stu_score<>value2 and stu_score<>value3 如果有一个value是null,则整个结果都为空
SQL> select id,stu_score from score where stu_score not in (90,87,null);
no rows selected
②、ANY
大于any,即大于最小值
小于any,即小于最大值
SQL> select id,stu_score from score where stu_score >any(75,50);
ID STU_SCORE
---------- ----------
1001 90
1002 87
1003 92
1004 79
1008 65
SQL> select id,stu_score from score where stu_score <any(75,50);
ID STU_SCORE
---------- ----------
1008 65
③、ALL
大于all,即大于最大值
小于all,即小于最小值
SQL> select id,stu_score from score where stu_score >all(75,50);
ID STU_SCORE
---------- ----------
1001 90
1002 87
1003 92
1004 79
SQL> select id,stu_score from score where stu_score <all(75,50);
no rows selected
写在select后(标量子查询)
SQL> select STU_SCORE,(select min(STU_SCORE) from score) from score;
STU_SCORE (SELECTMIN(STU_SCORE)FROMSCORE)
---------- -------------------------------
90 65
87 65
92 65
79 65
65 65