您的位置:首页 > 其它

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 ;

结果:

<
help_category_idnameparent_category_id
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
5Plugins34
6MBR33
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
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 ;

结果:

<
help_category_idnameparent_category_idurl
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
5Plugins34
6MBR33
7Control flow functions37
5Plugins34
6MBR33
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
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;

<
help_category_idnameparent_category_idurl
5Plugins34
6MBR33
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
调换顺序后,结果无序

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;

<
help_category_idnameparent_category_idurl
5Plugins34
6MBR33
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
5Plugins34
6MBR33
7Control flow functions37
同样 ,调换顺序后结果无序

所以为了保证数据的有序,需要在 最后那个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;

<
help_category_idnameparent_category_idurl
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
5Plugins34
5Plugins34
6MBR33
6MBR33
7Control flow functions37
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
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;

<
help_category_idnameparent_category_idurl
1Geographic0
2Polygon properties33
3WKT33
4Numeric Functions37
5Plugins34
6MBR33
7Control flow functions37
8Transactions34
9Help Metadata34
10Account Management34
Union,对两个结果集进行并集操作,不包括重复行

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: