陈宇 发表于 2021-8-15 19:34:23

多表查询

三表连接:方法一:三表隐藏内连接格式:select*from   表1 , 表2 ,表3   where表1.关联的字段=表3.关联字段and   表2.关联字段=表3.表3字段select*fromstudent as a , course as b , scasc wherea.stu_no=c.stu_no and b.c_no=c.c_no方法二:三表普通内连接格式:select*from表1 INNER JOIN表3   on   表1.关联的字段=表3.关联字段    inner join 表2on   表2.关联字段=表3.表3字段select*fromstudent as a INNER JOINscasc on a.stu_no=c.stu_noinner joincourse asbon    b.c_no=c.c_no方法三:三表普通左连接格式:select*from表1   left   JOIN表3   on   表1.关联的字段=表3.关联字段    left    join 表2on   表2.关联字段=表3.表3字段select*fromstudent as a left JOINscasc on a.stu_no=c.stu_noleft joincourse asbon    b.c_no=c.c_no 方法四:三表普通右连接格式:select*from表1   rightJOIN表3   on   表1.关联的字段=表3.关联字段    right    join 表2on   表2.关联字段=表3.表3字段select*fromstudent as a   right JOINscasc on a.stu_no=c.stu_noright    joincourse asbon    b.c_no=c.c_no 方五:先合两表,在两表合并成一表与第三个表合并selects.stu_no ,s.stu_name from ( selecta.stu_no ,stu_name,c_nofromstudent a,sc c wherea.stu_no=c.stu_no )s,courseas bwheres.c_no=b.c_no
页: [1]
查看完整版本: 多表查询