一.连接查询

  概念:根据两个表或多个表的列之间的关系,从这些表中查询数据

  目的:实现多表查询操作

  语法:From join_table join_type join_table[ON(join_condition)]

      join_table:连接的表名

      join_type:连接类型

      join_condition:连接条件

  连接类型:内连接,外连接,交叉连接

二.内连接

  1.等值连接

    概念:在连接条件中使用"="运算符,其查询结果中列出被连接表中的所有列,包括其中的重复列

    示例:

 create table student
 (
     studentId int not null primary key,
     studentName ) not null,
     classId int not null
 )
 create table class
 (
     classId int not null primary key,
     className ) not null
 )
 ,);
 ,);
 ,);
 ,);
 ,);
 ,);
 ,'逗哏班');
 ,'捧哏班');
 select * from student s inner join class c on s.classId = c.classId 

  2.不等值连接

    概念:在连接条件中使用除等号之外的运算符

    示例:

 select * from student s inner join class c on s.classId != c.classId

三.外链接

  1.左连接

    左表结构:

      

    右表结构:

      

    左连接查询:

select * from student s left join class c on s.classId = c.classId

     查询结果:

          

  总结:左连接:返回左表中的所有行,如果左表行在右表中没有匹配行,则结果中右表中的列返回控制

  2.右连接

     右连接查询: 

      

select * from student s right join class c on s.classId = c.classId

     查询结果:

      

   总结:右连接:恰与左连接相反,返回右表中的所有行,如果右表中行在左表中没有匹配行,则结果中左表中的列返回空值。

  3.全连接

    全连接查询

select * from student s right join class c on s.classId = c.classId

    查询结果

      

    总结:全连接:返回左表和右表中的所有行,当某行在另一表中没有匹配行,则灵异表中的列返回空值

四.交叉连接(笛卡尔积)

  1.不带where子句

    执行交叉连接:

select * from student cross join class

    查询结果:

      

  2.带where子句

    执行交叉连接:

select * from student s cross join class c where s.classId = c.classId

    查询结果:

  

    

  

Sql Server数据库之多表查询的更多相关文章

  1. 清空SQL Server数据库中所有表数据的方法(转)

    清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可能陷入 ...

  2. 清空SQL Server数据库中所有表数据的方法

    原文:清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可 ...

  3. sql server数据库备份单个表的结构和数据生成脚本

    1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...

  4. sql server数据库备份单个表的结构和数据生成脚本【转】

    1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...

  5. SQL server 数据库 操作及简单查询

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  6. sql server数据库将excel表中的数据导入数据表

    一般有两种方法可以实现,一种是直接写sql语句,另外一种是利用sqlserver的管理工具实现.这里介绍的是后面一种方法. 步骤: 一.准备数据 1.将excel表另存为文本格式,注意文本格式需为ta ...

  7. 在sql server数据库的一个表中如何查询共有多少字段

    select a.* from sys.columns a,sys.tables bwhere a.object_id = b.object_id and b.name = '要查的表名'

  8. SQL Server数据库阻塞,死锁查询

    sql 查询卡顿数据库 SELECT SPID=p.spid, DBName = convert(CHAR(20),d.name), ProgramName = program_name, Login ...

  9. sql server数据库查看锁表和解锁

    --查看锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys ...

随机推荐

  1. JavaWeb之搭建自己的MVC框架

    https://blog.csdn.net/anita9999/article/details/83378111 自己写一个mvc框架吧(一) https://www.cnblogs.com/heba ...

  2. Linux DNS 服务器安装、配置和维护

    每个 IP 地址都可以有一个主机名,主机名由一个或多个字符串组成,字符串之间用小数点隔开.有了主机名,就不要死记硬背每台 IP 设备的 IP 地址,只要记住相对直观有意义的主机名就行了.这就是 DNS ...

  3. vue实现原理

    1.数据监控(data):监听data属性: new Vue之后内部扫描data属性值,用 Object.defineProperty(obj,name,{ set:value=>{ obj[_ ...

  4. upstream timed out (10060: A connection attempt failed because the connected party did not properly respond

    openresty 错误日志报错内容: // :: [error] #: * upstream timed : A connection attempt failed because the conn ...

  5. python3-基础8

    模块与包 什么是模块 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. #在python中,模块的使用方式都是一样的,但其实细说的话,模块可以分为四个通用类别: 1 ...

  6. RN—Android 物理返回键监听

    A → B 使用 navigator 导航,用 goBack() 返回的时候传递参数 在 A 页面 this.props.navigation.navigate("B", { ca ...

  7. php中cookie和session的总结

    cookie: 设置cookie:  setcookie("name","zhang","time()+3600"); 参数一:属性名 参数 ...

  8. vscode之常用快捷键

    原文章地址: vscode: Visual Studio Code 常用快捷键 官方快捷键说明:Key Bindings for Visual Studio Code 主命令框 F1 或 Ctrl+S ...

  9. 01-JDK环境配置

    环境说明: Window server 2008 64位 jdk-7u80-windows-x64 apache-tomcat-7.0.57-windows-x64 1.安装JDK环境配置 JAVA_ ...

  10. java.lang.Long 类源码解读

    总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. // final修饰符 public final class Long extends Number i ...