存储过程中,怎么把多个select结果,合并成一个结果集返回?

book_xiny120 2012-05-24 12:39:08
select userid,username,price,vol from table_a ...
select userid,username,price,vol from table_b...


要让这两句形成一个结果集。怎么办?
...全文
845 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
重复的行是什么意思哩??另外如果两个结果是有排序的话。合并后的还是有排序的??还是说只是单纯的两个记录前后拼在一块??
[/Quote]

就是你上面那个语句和下面那个语句的结果中如果有数据是一模一样的行的话就只显示一行。
只能在下面那个查询语句后面跟order by
select userid,username,price,vol from table_a ...
union all
select userid,username,price,vol from table_b...order by ....

也可以

select * from(
select userid,username,price,vol from table_a ...
union all
select userid,username,price,vol from table_b...
)t order by ....
book_xiny120 2012-05-24
  • 打赏
  • 举报
回复

重复的行是什么意思哩??另外如果两个结果是有排序的话。合并后的还是有排序的??还是说只是单纯的两个记录前后拼在一块??
book_xiny120 2012-05-24
  • 打赏
  • 举报
回复
这么简单。。。遭受打击了。。。。我。。。
liwenguang_2009 2012-05-24
  • 打赏
  • 举报
回复
select userid,username,price,vol from table_a ...
union all
select userid,username,price,vol from table_b...

  • 打赏
  • 举报
回复

select userid,username,price,vol from table_a ...
union all--不去掉上下两语句的重复行
select userid,username,price,vol from table_b...


select userid,username,price,vol from table_a ...
union--去掉上下两语句的重复行
select userid,username,price,vol from table_b...
百年树人 2012-05-24
  • 打赏
  • 举报
回复
select userid,username,price,vol from table_a ...
union all
select userid,username,price,vol from table_b...

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧