Hopes

Start Here..

 

内连接、左外连接、右外连接、全外连接

--内连接



select distinct [Order Details] .ProductID,Products.ProductID

from [Order Details] inner join Products

on [Order Details] .ProductID=Products.ProductID


例如:

SQL语句:select * from student inner join course on student.ID=course.

IDinner join 是比较运算符,只返回符合条件的行。
此时相当于:select * from student,course where student.ID=course.ID


--三个表的内连接
select Suppliers.CompanyName,Products.ProductName,[Order Details] .ProductID,Products.ProductID

from Suppliers inner join Products

on Suppliers.SupplierID=Products.SupplierID

inner join [Order Details]

on Products.ProductID=[Order Details].ProductID





--左外连接
select Customers.City,Employees.City

from Customers left join Employees

on Customers.City=Employees.City

例如:

     select a.name, a.age, c.sex from b

      left join a on b.id=a.id

      left join c on b.id2=c.id2

      

又如:

insert into f as
select [fields]
from a
join b on a.a1=b.b1 and a.a2=b.b2
join c on c.c1=a.a1 and c.c2=b.b2
join d on d.d1=b.b1 and d.d3=b.b3
join e on e.e1=d.d1 and e.e2=a.a4
where a.a3='3' and b.b3='3' and c.c4='4' and d.d4='4' and e.e3='3';




--右外连接
select Customers.City,Employees.City

from Employees left join Customers

on Customers.City=Employees.City







--全外连接
select Customers.CompanyName,Orders.OrderID,Orders.OrderDate

from Orders full join Customers

on Customers.CustomerID=Orders.CustomerID


内连接:俗称等同连接,返回的结果集是两个表中所有相匹配的数据,舍弃不匹配的数据

SELECT

FROM <TABLE_1> INNER JOIN <TABLE_2>

ON <表达式>

外连接:系统生成的结果表中,不仅包括符合连接条件的行,还包括左表右表或者两个连接表中所有的数据行

左外连接:除了包括匹配行之外,还包括join关键字左表中不匹配的行,其中右表中缺少的属性值用NULL值来表示

右外连接:除了包括匹配行之外,还包括join关键字右表中不匹配的行,其中左表中缺少的属性值用NULL值来表示

全外连接:除了包括匹配行之外,还包括join关键字左右表中不匹配的行,其中左表或者右表缺少的属性值用NULL 值来表示









posted on 2012-09-28 14:11 ** 阅读(169) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

你好!

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

新闻档案

相册

收藏夹

C#学习

友情链接

搜索

最新评论

阅读排行榜

评论排行榜