zoukankan      html  css  js  c++  java
  • SQL数据库的多表查询

    多表查询分为 内、外连接

    外连接分为左连接(left join 或left outer join)、右连接(right join 或者 right outer join)、和完整外部连接 (full join 或者 full outer join)

    左连接(left join 或 left outer join)的结果就是left join子句中的左表的所有行,而不仅仅是链接列所匹配的行,如果左表中的某行在右表中没有匹配,则在相关联的结果行中右表的所有选择列均为空值(NULL)

    SQL语法 select * from table1 left join table2 on table1.条件列名 = table2.条件列名;

    注释: 显示的就是table1中的所有列和能匹配的列

    右连接(right join 或 right outer join )在这里不做多说这左连接很象但是是相反的,只说一下语法

    select *from table1 right join table2 on table1. 条件列= table2.条件列

    完全外部连接(full join 或 full outer join)

    显示左右表中的所有行,当某一个表中没有匹配的行时,则另一个表的选择列表列包含空值(NULL)如果有则显示全部数据

    SQL语法:

    select *from table1 full join table2 on table1.条件列名= table2.条件列名

    内连接:
    概念:内连接就是用比较运算符比较要用连接列的值的连接

    内连接(join 或者inner join )

    SQL语法:

    select *fron table1 join table2 on table1.条件列名 = table2.条件列名

    返回符合匹配条件的两表列

    等价于:

    select A* ,B* from table1 A ,table2 B where A.条件列名 =B.条件列名

    select *form table1 cross join table2 where table1.条件列名 = table2.条件列名(注: Cross join 后面不能跟on 只能用where)

    交叉连接(完全)

    概念:没有用where子句的交叉连接将产生连接所涉及的笛卡尔积第一个表的行数乘以第二个表的行数等于笛卡尔积和结果集的大小

    交叉连接: Cross join(不带条件where,如果带返回或显示的是匹配的行数)

    SQL语法:

    select *from table1 cross join table2

    如果有条件(where)

    select * from table1 cross join table2 where table1. 条件列名= table2.条件列名

    等价于

    select *from table1,table2 (不带where)

  • 相关阅读:
    通过点击切换文本框内容的脚本示例
    使用脚本改变树控件的行为
    javascript动态创建radio button元素支持IE/Firefox
    轻量级的向导控件MultiView
    客户端脚本简单实现Repeater的无刷新分页
    在非web site项目中引用Membership
    逐步认识C#四种判断相等的方法
    C#获取csv文件内容对逗号和引号分隔的处理
    JavaScript之 值类型 和 引用类型 Better
    JS call apply bind 方法的区别 Better
  • 原文地址:https://www.cnblogs.com/LangZiXiYan/p/6953435.html
Copyright © 2011-2022 走看看