Union和Union All的区别
2014-04-18 11:22
134 查看
使用mysql数据库中 mysql库中的 表 help_category
1>UNION 自动去除 重复数据,UNION ALL 不会
SELECT * FROM help_category t WHERE t.help_category_id<=7
UNION
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ;
结果:
SELECT * FROM help_category t WHERE t.help_category_id<=7
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ;
结果:
2>顺序问题
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION
SELECT * FROM help_category t WHERE t.help_category_id<=7;
调换顺序后,结果无序
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id<=7;
同样 ,调换顺序后结果无序
所以为了保证数据的有序,需要在 最后那个sql 中加上order by
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id;
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION
SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id;
Union,对两个结果集进行并集操作,不包括重复行
Union All,对两个结果集进行并集操作,包括重复行
最后把所有的sql脚本保留一下
1>UNION 自动去除 重复数据,UNION ALL 不会
SELECT * FROM help_category t WHERE t.help_category_id<=7
UNION
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ;
结果:
<
|
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ;
结果:
<
|
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION
SELECT * FROM help_category t WHERE t.help_category_id<=7;
<
|
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id<=7;
<
|
所以为了保证数据的有序,需要在 最后那个sql 中加上order by
SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10
UNION ALL
SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id;
<
|
UNION
SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id;
<
|
Union All,对两个结果集进行并集操作,包括重复行
最后把所有的sql脚本保留一下
SELECT * FROM help_category t WHERE t.help_category_id<=7 UNION SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ; SELECT * FROM help_category t WHERE t.help_category_id<=7 UNION ALL SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ; --================================================================================= SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 UNION SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id; SELECT * FROM help_category t WHERE t.help_category_id<=7 UNION SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ORDER BY help_category_id; --================================================================================= SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 UNION ALL SELECT * FROM help_category t WHERE t.help_category_id<=7 ORDER BY help_category_id; SELECT * FROM help_category t WHERE t.help_category_id<=7 UNION ALL SELECT * FROM help_category t WHERE t.help_category_id>=5 AND t.help_category_id<=10 ORDER BY help_category_id;
相关文章推荐
- union和union all的区别
- minus,union,union all,intersect 这几个的区别和用法
- UNION 和 UNION ALL 的区别与效率
- SQL中Union与Union All的区别
- Oracle中Union与Union All的区别
- union 和 union all的区别
- Union和Union All的区别
- Union与Union All的区别
- Union和Union All的区别
- Union和Union All的区别
- union、union all的用法和区别
- (转)图解SQL的inner join、left join、right join、full outer join、union、union all的区别
- MySql中UNION与UNION ALL的区别
- union和union all的区别
- Union和Union All的区别 .
- SQL语句中:UNION与UNION ALL的区别
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
- SQL语句中Union和Union all的区别
- sql 中union 和union all的区别
- 图解SQL inner join、left join、right join、full outer join、union、union all的区别