经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库运维 » MS SQL Server » 查看文章
sql学习笔记(三)—— 联表查询
来源:cnblogs  作者:CherishTheYouth  时间:2018/9/25 19:52:22  对本文有异议

上篇写了一些sql查询的知识,这篇接着写一下有关联表查询的知识。

既然是联表查询,那肯定得多个表啊,所以,我们先创建一个教师表,表名为 teacher,并且向表中插入数据。

准备工作:

创建表语句:

  1. create table teacher
  2. (
  3. id int primary key identity(1,1) not null,
  4. teaName varchar(50) not null,
  5. teaAge int,
  6. teaGender int,
  7. teaAddress nvarchar(50),
  8. majorId int ,
  9. subject nvarchar(50)
  10. )

 插入数据语句:

  1. 1 insert into teacher
  2. 2 (teaName,teaAge,teaGender,teaAddress,majorId,subject)
  3. 3 values
  4. 4 ('teacher-A',25,0,'武汉',4,'英语'),
  5. 5 ('teacher-B',26,0,'南京',2,'数学'),
  6. 6 ('teacher-C',27,0,'长沙',3,'物理'),
  7. 7 ('teacher-D',28,0,'汉口',6,'编程'),
  8. 8 ('teacher-E',29,0,'武昌',7,'计算机'),
  9. 9 ('teacher-F',30,0,'光谷',8,'政治'),
  10. 10 ('teacher-G',29,0,'金融港',1,'体育'),
  11. 11 ('teacher-H',28,0,'北京',0,'电路分析'),
  12. 12 ('teacher-I',27,0,'伦敦',4,'信号与系统')

 

显示结果:

 

 1.union

union语句用于合并两个或者多个select语句的结果集,用法是酱紫的:

  1. -- union 用法
  2. select 字段1,字段2,字段3,字段4 from 1
  3. union
  4. select 字段5,字段6,字段7,字段8 from 2
  5. union
  6. select 字段9,字段10,字段11,字段12 from 3
  7. ...
  8. -- 注意点:这里的 字段1-字段4、字段5-字段8、字段9-字段12....每个select语句
  9. -- 后的字段数必须相同,同时这些字段必须具有相同的数据类型

 注意咯,敲黑板:

select 后的字段,字段数量和字段类型必须保持一致

看个示例:

  1. 1 select teaName as 'name',teaAge as 'age',teaGender as 'gender' from teacher
  2. 2
  3. 3 union
  4. 4
  5. 5 select stuName ,stuAge,stuGender from student

结果如下:

 

 通过上图,可以看到,两个表中的数据显示在同一张表里了。

下面我们看一下,如果不按照字段数和类型相同的约定写,看情况如何:

(1)数目不一致:

(2)顺序不一致:

发现顺序不一致并不会出问题。

(3)字段类型不一致

第二次敲黑板,注意咯,union会把相同的数据省略掉,如果两张表中有的数据完全相同,则只会保留一个,而省略其他的行。

比如:我查询一下性别(因为性别只有2种值,所以查询结果应该只有两行),下面来看看实际情况:

  由图发现,确实如此,那么问题来了,union把重复的信息省略了,那么如果信息表里确实有两个人的信息完全相同,那要怎么弄呢,用union不就把信息遗漏了吗?

莫慌,这里我们只需要在union后面加上 all ,就可以解决这个问题了,这样,重复的信息就不会省略了。看下面:

从图中可以看出,所有的性别信息全都输出出来了。

 

2.inner join  内连接

内连接的查询条件比较苛刻哦,只有当查询条件完全匹配的时候才会有数据返回。

先写一下 inner join的用法:

  1. select 某某内容 from 主表
  2. inner join 联系表
  3. on 条件(主表.某字段 = 联系表.某字段)

 

然后看一个示例 —— 查出student表中stuAddress字段值等于teacher中teaAddress字段值的记录:

下面看一个匹配不上的示例:

 

 查询结果为空。

3.外连接

  外连接分为  左外连接、右外连接、全外连接三种情况。

   用法和内连接类似,如下所示:

  1. 1 select * from 左表 left join 右表 on 条件 -- 左外连接
  2. 2
  3. 3 select * from 左表 right join 右表 on 条件 -- 右外连接
  4. 4
  5. 5 full join on 条件 -- 全外连接

 (1)左外连接

 下面看示例:

从上面示例我们可以看到:

左表的内容默认是全部显示的,右表中若有匹配条件的数据,则在左表数据行的右边显示,若没有匹配数据,则显示数据为空(null).

 (2)右外连接

直接看示例:


右表的内容默认是全部显示的,左表中若有匹配条件的数据,则在右表数据行的左边显示,若没有匹配数据,则显示数据为空(null)。

显而易见的,左连接和右连接是一样的,只不过一个是完全保留左表的数据,一个是完全保留右表的数据。

 (3)全外连接

看示例:

全外连接,就相当于结合了左外和右外,把两张表里所有的信息都显示出来,不匹配的地方显示为 null,匹配的地方在同一排显示。

 

3.交叉连接 cross join

交叉连接会把左表中的每一行与右表中的每一行一一进行排列组合,然后全部显示出来,如果左表有6条记录,右表有7条记录,则查询后的结果应该有42条记录。

示例如下:

 

 联表查询就记录到这里啦,后面会看一下多表查询,嘿嘿,加油!

 最后附上我本章的sql脚本:

 

  1. 1 -- 联表查询 --
  2. 2 select * from student
  3. 3
  4. 4 -- 新建表teacher --
  5. 5
  6. 6 drop table teacher
  7. 7
  8. 8 create table teacher
  9. 9 (
  10. 10 id int primary key identity(1,1) not null,
  11. 11 teaName varchar(50) not null,
  12. 12 teaAge int,
  13. 13 teaGender int,
  14. 14 teaAddress nvarchar(50),
  15. 15 majorId int ,
  16. 16 subject nvarchar(50)
  17. 17 )
  18. 18
  19. 19 -- 向表中插入数据
  20. 20 insert into teacher
  21. 21 (teaName,teaAge,teaGender,teaAddress,majorId,subject)
  22. 22 values
  23. 23 ('teacher-A',25,0,'武汉',4,'英语'),
  24. 24 ('teacher-B',26,0,'南京',2,'数学'),
  25. 25 ('teacher-C',27,0,'长沙',3,'物理'),
  26. 26 ('teacher-D',28,0,'汉口',6,'编程'),
  27. 27 ('teacher-E',29,0,'武昌',7,'计算机'),
  28. 28 ('teacher-F',30,0,'光谷',8,'政治'),
  29. 29 ('teacher-G',29,0,'金融港',1,'体育'),
  30. 30 ('teacher-H',28,0,'北京',0,'电路分析'),
  31. 31 ('teacher-I',27,0,'伦敦',4,'信号与系统')
  32. 32
  33. 33 select teaName as 'name',teaAge as 'age',teaGender as 'gender' from teacher
  34. 34
  35. 35 union
  36. 36
  37. 37 select stuName ,stuGender,stuAddress from student
  38. 38
  39. 39 -- union 用法
  40. 40
  41. 41 select 字段1,字段2,字段3,字段4 from 1
  42. 42
  43. 43 union
  44. 44
  45. 45 select 字段5,字段6,字段7,字段8 from 2
  46. 46
  47. 47 union
  48. 48
  49. 49 select 字段9,字段10,字段11,字段12 from 3
  50. 50 ...
  51. 51 -- 注意点:这里的 字段1-字段4、字段5-字段8、字段9-字段12....每个select语句
  52. 52
  53. 53 -- 后的字段数必须相同,同时这些字段必须具有相同的数据类型
  54. 54
  55. 55 select stuGender from student
  56. 56
  57. 57 union all
  58. 58
  59. 59 select teaGender from teacher
  60. 60
  61. 61 -- 内连接 inner join
  62. 62
  63. 63 select * from student
  64. 64
  65. 65 inner join teacher on teacher.teaAge = student.stuAge
  66. 66
  67. 67 -- 左外连接
  68. 68
  69. 69 select * from student
  70. 70
  71. 71 left join teacher on teacher.teaAddress = student.stuAddress
  72. 72
  73. 73 -- 右外连接
  74. 74
  75. 75 select * from student
  76. 76
  77. 77 right join teacher on teacher.teaAddress = student.stuAddress
  78. 78
  79. 79 -- 全外连接
  80. 80 select * from student
  81. 81
  82. 82 full join teacher on teacher.teaAddress = student.stuAddress
  83. 83
  84. 84 -- cross join 交叉连接
  85. 85 select * from student
  86. 86
  87. 87 cross join teacher
联表查询 sql

 

 

 

我的邮箱:3074596466@qq.com

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号