oracle多列子查詢的實現
oracle多列子查詢是oracle數據庫中的一種查詢方式,下面就為您詳細介紹oracle多列子查詢的實現方法,希望能夠對您能夠有所幫助。
多列子查詢:
oracle子查詢一般都是返回單列,實際上數據庫對此并沒有進行限制,子查詢也可以返回多列.oracle多列子查詢的例子:
檢索每種產品在其類型中價格***的產品:
步驟:
1.通過分組查詢,統(tǒng)計每類產品的***價格:
- select product_type_id,min(Price) from products group by product_type_id
結果為:
PRODUCT_TYPE_ID MIN(PRICE)
--------------- ----------
1 19.95
2 13.95
3 12.99
4 10.99
13.49
2. 然后再掃描產品表中每行數據,看當前行的類別編輯與價格是否屬于上面子查詢中的一項;
完整查詢:
- select product_id,product_type_id,name,price
- from products
- where
- (product_type_id , price)
- in
- (select product_type_id , min(price)
- from
- products
- group by
- product_type_id)
查詢結果:
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
【編輯推薦】