海上月明

editer by sun
posts - 162, comments - 51, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

oracle多列子查询的实现[转]

Posted on 2010-12-15 13:22 pts 阅读(402) 评论(0)  编辑  收藏

From:http://database.51cto.com/art/201010/231773.htm

oracle多列子查询是oracle数据库中的一种查询方式,下面就为您详细介绍oracle多列子查询的实现方法,希望能够对您能够有所帮助。

多列子查询:

oracle子查询一般都是返回单列,实际上数据库对此并没有进行限制,子查询也可以返回多列.oracle多列子查询的例子:
检索每种产品在其类型中价格最低的产品:
步骤:

1.通过分组查询,统计每类产品的最低价格:

<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 3em !important; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 20px; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-image: url(http://www.51cto.com/images/art/images/010101bg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; color: #000000; line-height: 30px; background-position: initial initial; background-repeat: no-repeat repeat; border: 0px none initial;"><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">select product_type_id,min(Price) from products group by product_type_id </span></span></li></ol>


结果为:

PRODUCT_TYPE_ID MIN(PRICE)
--------------- ----------
              1                       19.95
              2                       13.95
              3                       12.99
              4                       10.99
                                       13.49

2. 然后再扫描产品表中每行数据,看当前行的类别编辑与价格是否属于上面子查询中的一项;
完整查询:

<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 3em !important; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 20px; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-image: url(http://www.51cto.com/images/art/images/010101bg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; color: #000000; line-height: 30px; background-position: initial initial; background-repeat: no-repeat repeat; border: 0px none initial;"><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">select product_id,product_type_id,name,price  </span></span></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: #000000; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">from products  </span></li><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">where  </span></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: #000000; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">(product_type_id , price)  </span></li><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">in  </span></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: #000000; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">(select product_type_id , min(price)  </span></li><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">from   </span></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: #000000; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">products  </span></li><li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: inherit; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">group by   </span></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; list-style-type: decimal; list-style-position: outside !important; list-style-image: initial; color: #000000; line-height: 24px; background-image: url(http://www.51cto.com/images/art/AppData/Roaming/Adobe/Dreamweaver%20CS4/zh_CN/Configuration/images/dt1.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: no-repeat no-repeat; border: 0px none initial;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px; border: 0px none initial;">product_type_id) </span></li></ol>

查询结果:
PRODUCT_ID PRODUCT_TYPE_ID NAME                                PRICE
---------- --------------- ------------------------------ ----------
         1               1 Modern Science                                               19.95
         4               2 Tank War                                                         13.95
         8               3 From Another Planet                                        12.99
         9               4 Classical Music                                                 10.99 


只有注册用户登录后才能发表评论。


网站导航: