SQL Server高级内容:子查询和表链接

数据库 SQL Server 数据库运维
表链接就像连接两张数据表的连线,线的两端是分别在两张表的特定字段上的。今天我们就将谈到表链接和子查询。

 1.子查询概念

   (1)就是在查询的where子句中的判断依据是另一个查询的结果,如此就构成了一个外部的查询和一个内部的查询,这个内部的查询就是自查询。

   (2)自查询的分类

  1)独立子查询

     ->独立单值(标量)子查询  (=)

 
  1. Select 
  2.   
  3.      testID,stuID,testBase,testBeyond,testPro  
  4.   
  5.  from Score 
  6.   
  7.      where stuID=( 
  8.       
  9.          select stuID from Student where stuName=’Kencery’ 
  10.   
  11.         ) 

   ->独立多值子查询  (in)

  1. Select 
  2.       testID,stuID,testBase,testBeyond,testPro  
  3.   from Score 
  4.       where stuID in
  5.           select stuID from Student where stuName=’Kencery’ 
  6.       ) 

   2)相关子查询

   (3)写子查询的注意事项

  1)子查询用一个圆括号阔气,有必要的时候需要为表取别名,使用“as 名字”即可。

2.表连接\

  (1)表链接就是将多个表合成为一个表,但是不是向union一样做结果集的合并操作,但是表链接可以将不同的表合并,并且共享字段。

  (2)表连接之交叉连接 (cross join)

    1)创建两张表

  1. use Test 
  2. go 
  3.  create table testNum1 
  4.  ( 
  5.      Num1 int 
  6.  ); 
  7.  create table testNum2 
  8.  ( 
  9.      Num2 int 
  10.  ); 
  11.  insert into testNum1 values(1),(2),(3) 
  12.  insert into testNum2 values(4),(5) 

    2) 执行交叉连接的SQL语句

      select * from testNum1 cross join testNum2

    3)注解

      交叉连接就是将第一张表中的所有数据与第二张表中的所有数据挨个匹配一次,构成一个新表。

    4)自交叉的实现

      执行插入SQL语句:

      insert into testNum1 values(4),(5),(6),(7),(8),(9),(0)

      执行自交叉的SQL语句:

      select t1.num1,t2.num2 from testNum1 as t1 cross join testNum2 as t2

    5)另外一种写法:

      select * from testNum1,testNum2不提倡使用,首先是有比较新的语法,缺陷是逗号不明确,并且这个语法与内连接和外连接都可以使用,如果使用join声明,那么语法错误的时候可以报错,但是使用这个语法,可能因为部分语法的错误,会被SQL Server解释为交叉连接而跳过这个语法的检查

   (3)表连接之内连接

     1)内链接是在交叉连接的基础之上添加一个约束条件

     2)语法:select * from 表1 inner join 表2 on 表1.字段=表2.字段

  1. Select  s1.stuID, 
  2.      s1.stuName, 
  3.      s1.stuSex, 
  4.      s2.testBase, 
  5.      s2.testBeyond  
  6.          from Student as s1 
  7.      inner join Score as s2 
  8.          on s1.stuID=s2.stuID 
  9.      where s1.stuIsDel=0; 

   (4)表连接之外连接

     1)执行下面的SQL语句

  1. create table tblMain 
  2.   ( 
  3.      ID int
  4.       name nvarchar(20), 
  5.       fid int 
  6.   ); 
  7.   create table tblOther 
  8.   ( 
  9.       ID int
  10.       name nvarchar(20) 
  11.   ) 
  12.   insert into tblMain values(1,'张三',1),(2,'李四',2) 
  13.   insert into tblOther values(1,'C++'),(2,'.net'),(3,'java'
  14.   select * from  
  15.       tblMain as t1 
  16.          inner join  
  17.       tblOther as t2 
  18.           on  
  19.       t1.fid=t2.id 

     2)在内连接的基础之上,在做一件事儿,就是将tblOther中的Java也显示出来,这时候就要使用到外连接,外连接有左外连接和右外连接。

     3)左连接和右连接有什么区别呢??区别就是**连接就是以**表为主表,在内连接的基础之上,将没有数据的那张表的信息还是要显示出来供用户查看,那么这个主表就是要显示的那张表。左外连接和右外连接的分别是在前面的这张表就是左表,在后面的那张表就是右表,左连接使用left join ,有连接使用right join。

     4)上面重新执行下面的SQL语句,就会显示出tblOther表中的Java。

原文链接:http://www.cnblogs.com/hanyinglong/archive/2013/03/06/2945380.html

【编辑推荐】

责任编辑:彭凡 来源: 博客园
相关推荐

2010-07-21 09:50:12

SQL Server子

2021-04-02 07:46:52

SQL Server数据库知识笔记

2010-06-30 15:24:49

SQL Server子

2011-08-30 11:04:30

链接查询内连接外连接

2011-08-12 09:30:04

SQL Server数高级SQL查询

2010-09-02 11:20:47

SQL删除

2011-08-18 13:31:44

SQL Server数子节点查询所有父节点

2023-12-16 13:14:00

SQL子查询技术

2015-07-22 17:27:41

SQL SERVER 链接

2009-02-16 09:35:19

业务规则链接SQL Server

2011-03-29 15:42:08

SQL Server链接

2010-08-23 08:43:49

SQL ServerMySQL

2011-04-28 09:49:56

SQLwith子查询

2011-04-15 11:43:24

SQL Server

2022-11-04 08:34:27

Oracle数据库

2010-10-21 10:28:13

SQL Server查

2009-07-06 18:18:41

SQL Server全

2010-11-09 10:00:37

SQL Server简

2010-10-21 12:16:11

SQL Server查

2021-02-06 13:45:59

SQL子查询数据库
点赞
收藏

51CTO技术栈公众号