小鸟求sql 单表多类型

phoebuswei 2009-02-14 04:03:14
问题是这样的
一个表有多类型,每个类型去出制定条记录
select top 1 name,.. from table where type=1
select top 1 name,.. from table where type=2
select top 1 name,.. from table where type=3
select top 1 name,.. from table where type=4
select top 3 name,.. from table where type=5
......
想怎么做能把它组合成一个结果集
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Roc_Lee 2009-02-14
  • 打赏
  • 举报
回复


select * from (select top 1 name,.. from table where type=1 order by n_date DESC,n_id DESC ) A
UNION ALL
select * from (select top 1 name,.. from table where type=2 order by n_date DESC,n_id DESC ) B
UNION ALL
select * from (select top 1 name,.. from table where type=3 order by n_date DESC,n_id DESC ) C
UNION ALL
select * from (select top 1 name,.. from table where type=4 order by n_date DESC,n_id DESC ) D
UNION ALL
select top 3 name,.. from table where type=5 order by n_date DESC,n_id DESC

肥龙上天 2009-02-14
  • 打赏
  • 举报
回复

select * from (select top 1 name,.. from table where type=1 order by n_date DESC,n_id DESC ) t1
UNION ALL
select * from (select top 1 name,.. from table where type=2 order by n_date DESC,n_id DESC ) t2
UNION ALL
select * from (select top 1 name,.. from table where type=3 order by n_date DESC,n_id DESC ) t3
UNION ALL
select * from (select top 1 name,.. from table where type=4 order by n_date DESC,n_id DESC ) t4
UNION ALL
select top 3 name,.. from table where type=5 order by n_date DESC,n_id DESC

肥龙上天 2009-02-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 phoebuswei 的回复:]
UNION ALL
和UNION 有什么不一样吗
对查询的性能有影响吗
[/Quote]


UNION 会自动取出重复记录
UNION ALL 会保存所有记录
UNION 因为要去除相同的数据,所以效率会稍微低一些
dawugui 2009-02-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 phoebuswei 的回复:]
是不是我写order by 的问题

select top 1 name,.. from table where type=1 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=2 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=3 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=4 order by n_date DESC,n_id DESC
UNION ALL

select top 3 name,.. from table where type=5 order by n_date DESC,n_id DESC
[/Quote]
select top 100 percent * from (select top 1 name,.. from table where type=1 order by n_date DESC,n_id DESC  ) t1
UNION ALL
select top 100 percent * from (select top 1 name,.. from table where type=2 order by n_date DESC,n_id DESC ) t2
UNION ALL
select top 100 percent * from (select top 1 name,.. from table where type=3 order by n_date DESC,n_id DESC ) t3
UNION ALL
select top 100 percent * from (select top 1 name,.. from table where type=4 order by n_date DESC,n_id DESC ) t4
UNION ALL
select top 100 percent * from (select top 3 name,.. from table where type=5 order by n_date DESC,n_id DESC ) t5
phoebuswei 2009-02-14
  • 打赏
  • 举报
回复
是不是我写order by 的问题

select top 1 name,.. from table where type=1 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=2 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=3 order by n_date DESC,n_id DESC
UNION ALL

select top 1 name,.. from table where type=4 order by n_date DESC,n_id DESC
UNION ALL

select top 3 name,.. from table where type=5 order by n_date DESC,n_id DESC


phoebuswei 2009-02-14
  • 打赏
  • 举报
回复
关键字 'UNION' 附近有语法错误。
phoebuswei 2009-02-14
  • 打赏
  • 举报
回复
UNION ALL
和UNION 有什么不一样吗
对查询的性能有影响吗
liangCK 2009-02-14
  • 打赏
  • 举报
回复
SELECT xx FROM xx
UNION ALL
SELECT xx FROM xx
UNION ALL
SELECT xx FROMxx

34,576

社区成员

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

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