Mysql(连接查询)

1、等值连接与非等值连接

当连接运算符为等号时为等值连接,当为其他运算符时为非等值连接。

    select studentno,sname                 --查询刘老师老师教过的学生的学号,姓名
    from student,teach_class,teacher
    where student.classno=teach_class.classno
    and teach_class.teacherno=teacher.teacherno
    and teacher.tname='刘老师'

2、自身连接

连接操作发生在一个表以其自身进行连接。

select teacher.*--查询同时教授“c1”和“c2”号课程的教师信息
from teach_class t1,teach_class t2,teacher 
where  t1.teacherno=teacher.teacherno
and t2.teacherno=teacher.teacherno
and t1.courseno='c1'
and t2.courseno='c2'

3、外连接

在连接操作中被舍弃的元组成为“悬浮元组”。

左外连接:只保留左边关系的悬浮元组。

右外连接:只保留右边关系的悬浮元组。

select course.*--查询没有任何学生选修的课程编号和课程名称及学分
from course left outer join score on (course.courseno=score.courseno)
where score.studentno is null

4、嵌套查询

ANY:某个值

ALL: 所有值

select studentno,sname,classno--查询入学成绩比本班平均入学成绩高的学生信息
from student s1
where point>(
select AVG(point)
from student s2
where s1.classno=s2.classno
)
原文地址:https://www.cnblogs.com/zhai1997/p/11373180.html