union 并列查询问题

che2piaopiao 2010-02-02 04:34:16
select * from productt where fuid in (select id from classtp where jibie<>1 and id<>144) order by fuid,sortid desc union
select * from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id<>144) order by fuid,sortid desc

SQL1 union SQL2

想要的结果:

sql1的数据
sql2的数据

但出现的结果却是。。

SQL1+SQL2 order by fuid,,,重新排序了。


求救了。 顺祝各位天天开心!


...全文
253 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sweet-Tang 2010-02-22
  • 打赏
  • 举报
回复

select * from
(
select *,1 as tbidx from SQL1
union all
select *,2 as tbidx from SQL2
) tt order by tbidx,fuid


select * from (select * from table1 order by id) a
union all
select * from (select * from table2 order by id) b

ChinaJiaBing 2010-02-21
  • 打赏
  • 举报
回复


union和union all 关键字都是将两个结果集合合并为一个,但这两者从使用和效率上来说都有所不同。

UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。实际大部分应用中是不会产生重复的记录,最常见的是过程表与历史表UNION。如:

select * from 表
union
select * from 表
cxmcxm 2010-02-21
  • 打赏
  • 举报
回复
select * from 
(select 1 as f1,* from productt where fuid in (select id from classtp where jibie <>1 and id <>144)
union
select 2,* from productt where fuid in(
select id from classtp
where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)
) as a
order by f1,fuid,sortid desc
老黎 2010-02-21
  • 打赏
  • 举报
回复

--不好意思,应该是这样
select * from (select top 10000000000 * from table1 order by id,...) a
union all
select * from (select top 10000000000 * from table2 order by id,...) b
引用 17 楼 kerafan 的回复:
楼主可以试一下这种写法
SQL codeselecttop10000000000*from table1orderby id,...unionallselecttop10000000000*from table2orderby id,...引用楼主 che2piaopiao 的回复:select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc  union  select * from productt where fuid in( select id from classtp where  id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144) order by fuid,sortid desc SQL1 union SQL2 想要的结果: sql1的数据 sql2的数据 但出现的结果却是。。 SQL1+SQL2 order by fuid,,,重新排序了。 求救了。 顺祝各位天天开心!
老黎 2010-02-21
  • 打赏
  • 举报
回复
楼主可以试一下这种写法

select top 10000000000 * from table1 order by id,...
union all
select top 10000000000 * from table2 order by id,...
引用楼主 che2piaopiao 的回复:
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc  union 
select * from productt where fuid in(
select id from classtp where  id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144) order by fuid,sortid desc

SQL1 union SQL2

想要的结果: sql1的数据 sql2的数据 但出现的结果却是。。 SQL1+SQL2 order by fuid,,,重新排序了。

求救了。 顺祝各位天天开心!
cl18850511 2010-02-21
  • 打赏
  • 举报
回复
union all 用这个 应该就不会排序,但是sql1和sql2会有重复的项,需要给sql2加where条件sql2.主键 not in sql1.主键
fwacky 2010-02-09
  • 打赏
  • 举报
回复
select A.*from
(
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144)
union
select * from productt where fuid in(
select id from classtp where id not in (
select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)
)A
order by A.fuid,A.sortid desc
ChinaITOldMan 2010-02-09
  • 打赏
  • 举报
回复
你在外面嵌套一层 再排序回来就行了
feegle_develop 2010-02-09
  • 打赏
  • 举报
回复
在外面排序

SELECT *
FROM
(
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144)-- order by fuid,sortid desc union
select * from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)-- order by fuid,sortid desc
) AS A
order by fuid,sortid desc
忆轩辕 2010-02-08
  • 打赏
  • 举报
回复


select * from
(
select *,1 as tbidx from SQL1
union all
select *,2 as tbidx from SQL2
) tt order by tbidx,fuid




jack15850798154 2010-02-07
  • 打赏
  • 举报
回复
select * from
(
select * from SQL1
union all
select * from SQL2
) tt order by fuid
hhiew 2010-02-07
  • 打赏
  • 举报
回复
上面的代码要把子查询中的ORDER BY全删了。
hhiew 2010-02-07
  • 打赏
  • 举报
回复
楼主是不是想SQL1 排序过后结果在上。SQL2排序后结果在下?而不是SQL1+SQL2一起排?试试这个,如果不想显示虚拟的列名,可以把*换成具体的字段名

select * from
(select * ,'sql1' as 'sql' from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc
union
select * ,'sql2' as 'sql2' from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144) )
order by sql,fuid,sortid desc
hhiew 2010-02-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 beirut 的回复:]
SQL codeselect*from
(select*from producttwhere fuidin (select idfrom classtpwhere jibie<>1and id<>144)orderby fuid,sortiddescunionselect*from producttwhere fuidin(select idfrom classtpwhere idnotin ?-
[/Quote]ORDER BY不能用在子查询中的。。。不行的。
yingzhilian2008 2010-02-06
  • 打赏
  • 举报
回复
order by fuid,sortid desc
幸运的意外 2010-02-06
  • 打赏
  • 举报
回复
去掉每个SQL中的排序,将最后的结果排序一次就可以了.
黄_瓜 2010-02-06
  • 打赏
  • 举报
回复
select * from 
(
select * from productt where fuid in (select id from classtp where jibie <>1 and id <>144) order by fuid,sortid desc union
select * from productt where fuid in(
select id from classtp where id not in (select distinct fuid from classtp where jibie=0) and jibie=1 and id <>144)
) a
order by fuid,sortid desc
ws_hgo 2010-02-02
  • 打赏
  • 举报
回复
看我3楼写的
ws_hgo 2010-02-02
  • 打赏
  • 举报
回复


select * from
(
select * from SQL1
union all
select * from SQL2
) tt
order by fuid
che2piaopiao 2010-02-02
  • 打赏
  • 举报
回复
您说的太深我听不懂啊。。


能说出来看下吗。。 谢谢了
加载更多回复(1)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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