zoukankan      html  css  js  c++  java
  • 数据库内连接、外连接与自连接

    1、内连接:

    所有满足条件的记录才会出现在结果中。

    select emp.name,dept.addr from emp,dept where emp.deptid=dept.id

    ->

    select emp.name dept.addr from emp inner join dept on emp.deptid=dept.id

    内连接上述两种写法都是可以的。其中第二种是正规写法。

    上面的sql是等值连接。除等值连接之外,还有非等值连接。

    2、外连接:

    不满足条件的记录也可以出现在结果中。其中左连接表示左边的表的记录全部都要出现在结果中,无论是否满足条件;右连接表示右侧的表的记录全部都要出现在结果中。

    mysql不支持full join。支持left join和right join。所以用left join的结果union all 上right join的结果,就可以模拟full join的结果。

    左侧的表称为主表,右侧的表称为从表。

    外连接中,主表的所有记录都会显示出来。

    3、自连接:

    自连接是说一个表,连接到自己。

    select t1.name as '当前类别',t2.name as '父类别'

    from tree t1 left join tree t2 on t1.pid=t2.id

    上面的这个例子就是自连接。

  • 相关阅读:
    单片机编程积累算法
    关于GSM基站定位
    GSM模块fibocom G510使用记录
    指爱 打字比赛记录
    硬件和软件工程师
    GPS模块启动模式说明
    阻容降压电路分析
    饮水机电路-工作剖析
    跑步,去
    day01 IT知识架构,操作系统简介
  • 原文地址:https://www.cnblogs.com/lyugeyi1030/p/8038457.html
Copyright © 2011-2022 走看看