随笔 - 224  文章 - 41  trackbacks - 0
<2010年6月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

原文地址:http://www.pin5i.com/showtopic-21996.html

 
在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下:
  1. SELECT [Id],[Name],[Comment] FROM [Product1]
  2. UNION
  3. SELECT [Id],[Name],[Comment] FROM [Product2]
复制代码
上面的代码可以实现将从Product1和Product2两张表合并成一个表,如果您只是希望合并两张表中符合特定条件的记录抑或是合并两张表各自的前N条记录,那么您的代码可能会像下面这样写:
  1. SELECT [Id],[Name],[Comment] FROM [Product1] WHERE LEN([Name]) > 5
  2. UNION
  3. SELECT [Id],[Name],[Comment] FROM [Product2] WHERE [Id] IN (11,20) AND [Comment] IS NOT NULL


  4. SELECT TOP N [Id],[Name],[Comment] FROM [Product1]
  5. UNION

  6. SELECT TOP N [Id],[Name],[Comment] FROM [Product2]
复制代码
This is so easy!但是假如您希望从包含Type字段的某表中根据Type分别随机筛选N条记录并将结果合并成一张表,您可能会像下面这样写:
  1. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE1' ORDER BY NEWID()
  2. UNION
  3. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE2' ORDER BY NEWID()
  4. UNION
  5. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE3' ORDER BY NEWID()
  6. UNION
  7. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE4' ORDER BY NEWID()
  8. UNION
  9. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE5' ORDER BY NEWID()
  10. UNION
  11. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE6' ORDER BY NEWID()
  12. UNION
  13. SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE7' ORDER BY NEWID()
复制代码
在查询分析器中执行如上语句会报错,这个问题起初会令您觉得UNION在这方面似乎有点软弱,难道UNION和ORDER BY就不能共存吗?当然可以,下面的代码或许能实现与上面代码希望实现的相同功能:
  1. SELECT * FROM
  2.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE1' ORDER BY NEWID()) AS [Product1]
  3.     UNION
  4.     SELECT * FROM
  5.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE2' ORDER BY NEWID()) AS [Product2]
  6.     UNION
  7.     SELECT * FROM
  8.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE3' ORDER BY NEWID()) AS [Product3]
  9.     UNION
  10.     SELECT * FROM
  11.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE4' ORDER BY NEWID()) AS [Product4]
  12.     UNION
  13.     SELECT * FROM
  14.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE5' ORDER BY NEWID()) AS [Product5]
  15.     UNION
  16.     SELECT * FROM
  17.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE6' ORDER BY NEWID()) AS [Product6]
  18.     UNION
  19.     SELECT * FROM
  20.         (SELECT TOP N [Id],[Name],[Comment] FROM [Product] WHERE [Type]='TYPE7' ORDER BY NEWID()) AS [Product7]
复制代码
代码看起来有些繁琐,或许针对这个问题有更见简洁明快的方法,我在这里也只是抛砖引玉,希望各位大侠能相处更为经典的代码,待续~

注意:其实很多时候多可以用到这种技巧。
posted on 2011-03-21 15:25 漂漂 阅读(295) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理