首页 > 数据库技术 > 详细

mysql学习之内、外连接语句

时间:2014-09-04 23:31:30      阅读:411      评论:0      收藏:0      [点我收藏+]

名词:

内连接:自然连接,只有两个相匹配的行才能在结果集中显示

外连接:左外连接、右连接、全外连接

内连接,只显示满足where后条件的列  select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id 

左外连接,select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id     左列为主,右表为副表。

右外连接, select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id      左列为副表,右表为主表

全外连接,select   a.*,b.*   from   a   full   join   b     on   a.id=b.parent_id  返回左表和右表的全部值 ,当某表在一另外一表无匹配值 ,则为空

注意:mysql不支持Full join,

实践操作:1、内连接  SELECT  student.name,teacher.high FROM student INNER JOIN teacher ON student.id=teacher.id; 

              2、左外连接  SELECT  student.name,teacher.high FROM student LEFT JOIN teacher ON student.id=teacher.id;

              3、右外连接  SELECT  student.name,teacher.high FROM student RIGHT JOIN teacher ON student.id=teacher.id;

操作过程中碰到的问题:出现1066号错误 ,刚开始为是不能显示相同的列,实际是语句中缺少 on 

mysql学习之内、外连接语句

原文:http://www.cnblogs.com/xuebaobao/p/3956999.html

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