在一个项目中要新增一个下拉搜索条件,要求开通区域在上面,未开通的区域在下面显示
数据来源是数据库
select * from FromAreaInfo
使用union
select '全部' as CtiyName union
select CtiyName from FromAreaInfo where Enable=1 union
select '未开通区域' as CtiyName union
select CtiyName from FromAreaInfo where Enable=0
发现不是自己想要的顺序
使用union all
select '全部' as CtiyName union all
select CtiyName from FromAreaInfo where Enable=1 union all
select '未开通区域' as CtiyName union all
select CtiyName from FromAreaInfo where Enable=0
发现这才是符合要求的
总之:union 会对查询的结果集进行默认排序,过滤重复的数据,而union all 则不会对所有结果集进行排序,它的作用是合并结果集。