mssql2000中 关于 union 与 order by

hflkl1314 2011-04-23 05:09:29
mssql2000中
如何同时使用 union 与 order by

...全文
130 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy_liucj 2011-04-26
  • 打赏
  • 举报
回复
应该说union all中是用order by没有任何意义 =。=
jyh070207 2011-04-26
  • 打赏
  • 举报
回复
--里面的1,0只是排序优先级用
select top 12 a,b,c from
(select top 12 a,b,c,1 as seq from 竞价产品表
order by 价格 desc--前12个出价最高竞价产品
union
select a,b,c,0 as seq from 产品表
) t
order by seq desc
cd731107 2011-04-26
  • 打赏
  • 举报
回复
/*
--比如要排在第一位这条记录的A字段是唯一的(和其他字段不一样),这个值比如是'max',
那么在newid()前加一个排序规则,如下所示
*/

order by case when A='mzx' then 0 else 1 end,newid()
hflkl1314 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 jyh070207 的回复:]
--不知是不是这样?最好有示例数据及期望的结果出来.
select t.* from
(select top 10 a,b,c from tablename
union
select a,b,c from tablename2
union
select a,b,c from tablename3) t
order by newid()
[/Quote]

确切的是这样的。
产品表,以及产品对应的竞价位置。

一个产品可以同时在多个竞价位置显示。


我想实现的目的是这样的

比如一个位置可以放置12个产品,我先显示竞价的位置里面的产品,然后再随机显示其他产品



这样的显示方式要如何实现
jyh070207 2011-04-26
  • 打赏
  • 举报
回复
--不知是不是这样?最好有示例数据及期望的结果出来.
select t.* from
(select top 10 a,b,c from tablename
union
select a,b,c from tablename2
union
select a,b,c from tablename3) t
order by newid()
benjoy 2011-04-26
  • 打赏
  • 举报
回复
我觉得你最好是根据你的具体需求去考虑SQL怎么写,呵呵。你的说法太笼统,当然就是先union,然后再来一个order就搞定了。
hflkl1314 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 houhu_cs 的回复:]
order by 返回的是游标而非临时表,是不能在子查询中使用的
[/Quote]

的确是这样,有什么变革的办法吗
hflkl1314 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 maco_wang 的回复:]
SQL code
select * from
(select top 10 * from tablename order by id) aa
order by newid()
[/Quote]

这个不涉及 union
幸运的意外 2011-04-26
  • 打赏
  • 举报
回复
先执行union最后执行order by
myweb123 2011-04-26
  • 打赏
  • 举报
回复
我也一直搞不清楚这个。
andy_liucj 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fredrickhu 的回复:]
SQL code
select top 100 percent * from tb order by id
union all
select top 100 * from ta order by ida
[/Quote]
++

虽然order by 返回游标,但是100 percent却又是集合
houhu_cs 2011-04-25
  • 打赏
  • 举报
回复
order by 返回的是游标而非临时表,是不能在子查询中使用的
叶子 2011-04-25
  • 打赏
  • 举报
回复
select * from
(select top 10 * from tablename order by id) aa
order by newid()


hflkl1314 2011-04-25
  • 打赏
  • 举报
回复
首先 order by 是要在子查询中使用,不是在结果集中使用

其次,我是用 order by newid()进行排序的,所以,显示结果字段中是不会出现排序字段的

有其他办法吗
--小F-- 2011-04-23
  • 打赏
  • 举报
回复
select top 100 percent * from tb order by  id
union all
select top 100 * from ta order by ida
叶子 2011-04-23
  • 打赏
  • 举报
回复

select 4 as a union all
select 3 union all
select 1 union all
select 7 union all
select 2 union all
select 5 order by a
/*
a
-----------
1
2
3
4
5
7
*/
Billy 2011-04-23
  • 打赏
  • 举报
回复
select 1 as a ,'01' as b
union
select 2,'02' order by a desc
gw6328 2011-04-23
  • 打赏
  • 举报
回复
神马情况

34,590

社区成员

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

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