我们来看个例子:
内连接就是普通的查询,例如
1.加inner join的:
SELECT S.SName,C.CourseID,C.Grade
From Sc AS C
INNER JOIN Students AS S
ON C.StudentID = S.SNo
2.不加inner join的
SELECT Students.SName, Sc.CourseID, Sc.Grade
FROM Students,Sc
WHERE Students.SNo = Sc.StudentID
外连接与普通连接的区别:
普通连接只输出满足连接条件的元组,外连接操作以指定表为连接主题,将主体表中不满足连接条件的元组一并输出。
外连接又分为左连接和右连接:
左连接:SELECT S.SName,C.CourseID,C.Grade
From Students AS S
INNER JOIN Sc AS C
ON C.StudentID <> S.SNo
SELECT S.SName,C.CourseID,C.Grade
From Sc AS C
LEFT JOIN Students AS S
ON C.StudentID = S.SNo
左连接就是以from左边为主,拿上面两个例子来说,第一种是以学生(students)为主,假如学生成绩为空,也能查询到学生信息,只不过成绩为空。而第二种则是以成绩为主(sc)所以先查学生成绩,如果成绩为空就算学生存在,也查不到记录。
右连接:SELECT Titles.Title_id, Titles.Title, Publishers.Pub_name
FROM titles
RIGHT OUTER JOIN Publishers
ON Titles.Pub_id = Publishers.Pub_id
右连接就是以from右边为主,与左连接类似。