`
jishublog
  • 浏览: 869093 次
文章分类
社区版块
存档分类
最新评论

Union和Union All的区别

 
阅读更多

假设我们有一个表Student,包括以下字段与数据:

  1. droptablestudent;
  2. createtablestudent
  3. (
  4. idintprimarykey,
  5. namenvarchar2(50)notnull,
  6. scorenumbernotnull
  7. );
  8. insertintostudentvalues(1,'Aaron',78);
  9. insertintostudentvalues(2,'Bill',76);
  10. insertintostudentvalues(3,'Cindy',89);
  11. insertintostudentvalues(4,'Damon',90);
  12. insertintostudentvalues(5,'Ella',73);
  13. insertintostudentvalues(6,'Frado',61);
  14. insertintostudentvalues(7,'Gill',99);
  15. insertintostudentvalues(8,'Hellen',56);
  16. insertintostudentvalues(9,'Ivan',93);
  17. insertintostudentvalues(10,'Jay',90);
  18. commit;

首先,我们来看一下UNION的例子:


  1. SQL>select*
  2. 2fromstudent
  3. 3whereid<4
  4. 4union
  5. 5select*
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 4Damon90
  15. 5Ella73
  16. SQL>

如果换成Union All连接两个结果集,则结果如下:

  1. SQL>select*
  2. 2fromstudent
  3. 3whereid<4
  4. 4unionall
  5. 5select*
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 3Cindy89
  15. 4Damon90
  16. 5Ella73
  17. 6rowsselected.

可以看到,Union和Union All的区别之一在于对重复结果的处理。

接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。

  1. SQL>select*
  2. 2fromstudent
  3. 3whereid>2andid<6
  4. 4union
  5. 5select*
  6. 6fromstudent
  7. 7whereid<4
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 4Damon90
  15. 5Ella73
  16. SQL>select*
  17. 2fromstudent
  18. 3whereid>2andid<6
  19. 4unionall
  20. 5select*
  21. 6fromstudent
  22. 7whereid<4
  23. 8;
  24. IDNAMESCORE
  25. --------------------------------------------------
  26. 3Cindy89
  27. 4Damon90
  28. 5Ella73
  29. 1Aaron78
  30. 2Bill76
  31. 3Cindy89
  32. 6rowsselected.

可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。

那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:

  1. SQL>selectscore,id,name
  2. 2fromstudent
  3. 3whereid<4
  4. 4union
  5. 5selectscore,id,name
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. SCOREIDNAME
  10. --------------------------------------------------
  11. 735Ella
  12. 762Bill
  13. 781Aaron
  14. 893Cindy
  15. 904Damon

可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。

那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:

  1. selectscore,id,name
  2. fromstudent
  3. whereid>2andid<7
  4. union
  5. selectscore,id,name
  6. fromstudent
  7. whereid<4
  8. union
  9. selectscore,id,name
  10. fromstudent
  11. whereid>8
  12. orderbyiddesc

order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。

其他的集合操作符,如Intersect和Minus的操作和Union基本一致,这里一起总结一下:

Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All,对两个结果集进行并集操作,包括重复行,不进行排序;

Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;

Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

可以在最后一个结果集中指定Order by子句改变排序方式。

假设我们有一个表Student,包括以下字段与数据:

  1. droptablestudent;
  2. createtablestudent
  3. (
  4. idintprimarykey,
  5. namenvarchar2(50)notnull,
  6. scorenumbernotnull
  7. );
  8. insertintostudentvalues(1,'Aaron',78);
  9. insertintostudentvalues(2,'Bill',76);
  10. insertintostudentvalues(3,'Cindy',89);
  11. insertintostudentvalues(4,'Damon',90);
  12. insertintostudentvalues(5,'Ella',73);
  13. insertintostudentvalues(6,'Frado',61);
  14. insertintostudentvalues(7,'Gill',99);
  15. insertintostudentvalues(8,'Hellen',56);
  16. insertintostudentvalues(9,'Ivan',93);
  17. insertintostudentvalues(10,'Jay',90);
  18. commit;

首先,我们来看一下UNION的例子:


  1. SQL>select*
  2. 2fromstudent
  3. 3whereid<4
  4. 4union
  5. 5select*
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 4Damon90
  15. 5Ella73
  16. SQL>

如果换成Union All连接两个结果集,则结果如下:

  1. SQL>select*
  2. 2fromstudent
  3. 3whereid<4
  4. 4unionall
  5. 5select*
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 3Cindy89
  15. 4Damon90
  16. 5Ella73
  17. 6rowsselected.

可以看到,Union和Union All的区别之一在于对重复结果的处理。

接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。

  1. SQL>select*
  2. 2fromstudent
  3. 3whereid>2andid<6
  4. 4union
  5. 5select*
  6. 6fromstudent
  7. 7whereid<4
  8. 8;
  9. IDNAMESCORE
  10. --------------------------------------------------
  11. 1Aaron78
  12. 2Bill76
  13. 3Cindy89
  14. 4Damon90
  15. 5Ella73
  16. SQL>select*
  17. 2fromstudent
  18. 3whereid>2andid<6
  19. 4unionall
  20. 5select*
  21. 6fromstudent
  22. 7whereid<4
  23. 8;
  24. IDNAMESCORE
  25. --------------------------------------------------
  26. 3Cindy89
  27. 4Damon90
  28. 5Ella73
  29. 1Aaron78
  30. 2Bill76
  31. 3Cindy89
  32. 6rowsselected.

可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。

那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:

  1. SQL>selectscore,id,name
  2. 2fromstudent
  3. 3whereid<4
  4. 4union
  5. 5selectscore,id,name
  6. 6fromstudent
  7. 7whereid>2andid<6
  8. 8;
  9. SCOREIDNAME
  10. --------------------------------------------------
  11. 735Ella
  12. 762Bill
  13. 781Aaron
  14. 893Cindy
  15. 904Damon

可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。

那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:

  1. selectscore,id,name
  2. fromstudent
  3. whereid>2andid<7
  4. union
  5. selectscore,id,name
  6. fromstudent
  7. whereid<4
  8. union
  9. selectscore,id,name
  10. fromstudent
  11. whereid>8
  12. orderbyiddesc

order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。

其他的集合操作符,如Intersect和Minus的操作和Union基本一致,这里一起总结一下:

Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All,对两个结果集进行并集操作,包括重复行,不进行排序;

Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;

Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

可以在最后一个结果集中指定Order by子句改变排序方式。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics