先决条件– MS SQL Server简介
1.左加入:
联接仅合并两个表的集合。当用户只想提取左表的数据时,将使用左联接。左联接不仅组合左表的行,而且组合右表旁边的行。
句法 –
select select_list
from table1 left join table2 on join_predicate
(OR)
select *
from table1 right join table2
2.正确加入:
右联接将右表的数据和与两个表匹配的行组合在一起。
句法 –
select select_list
from table1 right join table2 on join_predicate
(OR)
select *
from table1 right join table2
例子 –
第一个表是“课程”表,被认为是左表,第二个表是“学生”表,被认为是右表。
Name | Course | Age |
---|---|---|
Aisha | CSE | 19 |
Vani | ECE | 18 |
Mina | EEE | 18 |
Name | Rollno | Age |
---|---|---|
Aisha | 111 | 19 |
Vani | 112 | 18 |
Mina | 113 | 18 |
1.左加入:
左联接将应用于“课程”和“学生”表,下表是结果集。
select name, course
from c.course left join s.student on c.age = s.age
Name | Course | Name | Course |
---|---|---|---|
Aisha | CSE | Aisha | NULL |
Vani | ECE | Vani | NULL |
Mina | EEE | Mina | NULL |
显示左表及其在右表上的相应匹配行。如果用户只想在左表中显示行,则可以在查询中使用where子句。左联接通常最多用于两个表,但是对于SQL Server,它也可以用于多个表。 2.正确加入:
右连接将应用于“课程”和“学生”表,下表是结果集。
select name, rollno
from c.course right join s.student on c.age = s.age
Name | Rollno | Name | Rollno |
---|---|---|---|
Aisha | 111 | Aisha | NULL |
Vani | 112 | Vani | NULL |
Mina | 113 | Mina | NULL |
如果表没有公共行,则将这些行显示为NULL。正确的联接也可以用于多个表。