首页 > 其他 > 详细

多表连接查询

时间:2019-09-22 17:12:36      阅读:104      评论:0      收藏:0      [点我收藏+]

一:交叉连接

  语法: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;

    

多表连接查询

原文:https://www.cnblogs.com/W19990605/p/11568100.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!