SQL 链接 (代码)

************************
*              连接查询              *
************************

**************
*       JOIN        *
**************

实现两表连接:
SELECT 学生信息表.编号,学生信息表.姓名,成绩表.成绩
FROM 学生信息表,成绩表
WHERE 成绩表.编号=学生信息表.编号
--------------------------------------
不指明关系的两表连接:
SELECT 学生信息表.编号,学生信息表.姓名,成绩表.成绩
FROM 学生信息表,成绩表
--------------------------------------
实现三个表连接:
SELECT 学生信息表.编号,学生信息表.姓名,
成绩表.姓名,成绩表.成绩,班级表.班级
FROM 学生信息表,成绩表,班级表
WHERE 成绩表.编号=学生信息表.编号
AND 学生信息表.班级=班级表.班级
--------------------------------------
实现多个表的连接:
SELECT 学生信息表.编号,学生信息表.姓名,
成绩表.姓名,成绩表.成绩,班级表.班级,
学院表.学院
FROM 学生信息表,成绩表,班级表,学院表
WHERE 成绩表.编号=学生信息表.编号
AND 学生信息表.班级=班级表.班级
AND 班级表.学院=学院表.学院
--------------------------------------
使用自连接:
SELECT DISTINCT 成绩表1.姓名,成绩表1.科目,成绩表1.成绩
FROM 成绩 AS 成绩表1,成绩 AS 成绩表2
WHERE 成绩表1.学号=成绩表2.学号
AND 成绩表2.成绩<60
ORDER BY 成绩表1.姓名
使用自然连接连接两个数据表:
SELECT 教师编号,教师姓名,教师年龄,小组名,组成绩
FROM 教师信息表 NATURAL JOIN 小组信息表
--------------------------------------
使用WHERE 子句实现等值连接:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gMark
FROM teachers AS S,
Groups AS T
WHERE S.gid=t.gid
--------------------------------------
使用内连接等值连接实现查询:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
INNER JOIN Groups AS T
OM S.gid=T.gid
ORDER BY TNO
--------------------------------------
排序内连接的查询结果:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
INNER JOIN Groups AS T
ON S.gid=T.gid
ORDER BY TNO
--------------------------------------
使用内连接不等值连接实现查询:
SELECT S.TNO,S.sName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
INNER JOIN Groups AS T
ON S.gid<>T.gid
ORDER BY TNO
--------------------------------------
通过内连接实现多表链接:
SELECT student.stNo,student.stName,
achievement.suName,achievement.achieve,Class.cName
FROM student
INNER JOIN achievement
ON achievement.stNo=student.stNo
INNER JOIN Class
ON student.stpNo=Class.cId
--------------------------------------
在内连接中使用聚合函数:
SELECT R.stNo,R.stName,
MAX(achieve) AS 最高科目成绩,
MIN(achieve) AS 最低科目成绩,
AVG(achieve) AS 平均成绩
FROM student AS R
INNER JOIN achievement AS S
ON R.stNo=S.stName
GROUP BY R.stNo,R.stName
ORDER BY R.stNo
--------------------------------------

***********************
*               外连接               *
***********************

使用左外连接:
SELECT S.TNO,S.TName,S.tAge,T.gid,T,gName,T.gMark
FROM teachers AS S
LEFT OUTER JOIN Groups AS T
ON S.gid=T.gid
ORDER BY TON
或(使用"*="):
SELECT S.TNO,S.TName,S.tAge,T.gid,TgName,T.gMark
FROM teachers AS S, Groups AS T
WHERE S.gid*=T.gid
ORDER BY TNO
--------------------------------------
/*    LEFT OUTER JOIN / *=    */
使用右外连接实现查询:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
RIGHT OUTER JOIN Groups AS T
ON S.gid=T.gid
ORDER BY TON
或(使用"=*"):
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S, Groups AS T
WHERE S.gid=*T.gid
ORDER BY TNO
--------------------------------------
/*    RIGHT OUTER JOIN / =*   */
使用全外连接实现查询:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
FULL OUTER JOIN Groups AS T
ON S.gid=T.gid
ORDER BY TNO
--------------------------------------
在左外连接中使用聚合函数:
SELECT R.stNo,R.stName,
MAX(achieve) AS 最高科目成绩,
MIN(achieve) AS 最低科目成绩,
AVG(achieve) AS 平均成绩
FROM student AS R
LEFT OUTER JOIN achievement AS S
ON R.stNo=S.stNo
GROUP BY R.stNo,R.stName
ORDER BY R.stNo
--------------------------------------
在右外连接中使用聚合函数:
SELECT R.stNo,R.stName,
MAX(avhieve) AS 最高科目成绩,
MIN(achieve) AS 最低科目成绩,
AVG(achieve) AS 平均成绩
FROM student AS R
RIGHT OUTER JOIN achievement AS S
ON R.stNo=S.stNo
GROUP BY R.stNo,R.stName
ORDER BY R.stNo
--------------------------------------
在全连接中使用聚合函数:
SELECT R.stNO,R.stName,
MAX(achieve) AS 最大科目成绩,
MIN(achieve) AS 最小科目成绩,
AVG(achieve) AS 平均成绩
FROM student AS R
FULL OUTER JOIN achievement AS S
ON R.stNo=S.stNo
GROUP BY R.stNo,R.stName
ORDER BY R.stNo
************************
*              交叉连接              *
************************

*****************
*    CROSS JOIN     *
*****************

交叉连接返回结果是两个数据表的并集

使用交叉连接实现查询:
SELECT S.TNO,S.tName,S.tAge,T.gid,T.gName,T.gMark
FROM teachers AS S
CROSS JOIN Groups AS T
ORDER BY TNO

**************************
*         多表连接的综合应用         *
**************************

使用内连接与左外连接实现查询:
SELECT distinct R.stNo,R.stName,S.cName,
T.suName,T.achieve
FROM student AS R
INNER JOIN achievement AS T
ON R.stNo=T.stNo
LEFT OUTER JOIN Class AS S
ON R.stpNo=S.cId
ORDER BY R.stNo
--------------------------------------
使用内连接与右外连接实现查询:
SELECT distinct R.stNo,R.stName,S.cName,
T.suName,T.achieve
FROM student AS R
INNER JOIN achievement AS T
ON R.stNo=T.stNo
RIGHT OUTER JOIN Class AS S
ON R.stpNo=S.cId
ORDER BY R.stNo
--------------------------------------
UNION 集合运算与多表连接应用:
SELECT stNo,stName,Sage FROM student
UNION
SELECT S.TNO,S.tName,S.tAge
FROM teachers AS S
INNER JOIN Groups AS T
ON S.gid=T.gid

posted @ 2011-08-11 11:56  rui_gao  阅读(290)  评论(0编辑  收藏  举报