多表连接查询

一:交叉连接

  语法:select 表名1.字段名,表名2.字段名......

     from 表名1,表名2......

     where 表名1.字段名=表名2.字段名;(字段之间有联系)

  例子:select a.studentname,b classname

     from studentinfo a,classinfo b

     where a.classid=b.classid;

二:内连接

  语法:select 表名1.字段名,表名2.字段名......

     from 表名1

     INNER IN 表名2  ON  表名1.字段名=表名2.字段名

     where 条件;

  例子:select  a.studentname,b.score,c.classname

     from student a

        INNER IN score b ON a.id=b.id

       INNER IN class c ON b.classid=c.classid

            where b.score>60 ORDER BY b.score DESC;

三:外连接

  1)左外连接:

    LEFT JOIN 左边的表显示全部的记录,没记录的显示null,右边显示与左边有联系的信息

    语法:select  表名1.字段名,表名2.字段名......

       from 表名1 

       LEFT JOIN 表名2 

       ON 表名1.字段名=表名2.字段名;(字段之间有联系)

    例子:select a.studentname,b.score

       from student a

       LEFT JOIN EXAM b

       ON a.id=b.id;

  2)右外连接

    RIGHT JOIN 右边的表显示全部的记录,没记录的显示null,左边显示与左边有联系的信息

    语法:select  表名1.字段名,表名2.字段名......

       from 表名1 

       RIGHT JOIN 表名2 

       ON 表名1.字段名=表名2.字段名;(字段之间有联系)

    例子:select a.studentname,b.score

       from student a

       RIGHT JOIN EXAM b

       ON a.id=b.id;

    

posted @ 2019-09-22 16:53  意中人是个盖世英雄  阅读(110)  评论(0编辑  收藏  举报