2009-11-23 33 views
0

是否有可能作出同樣的查詢中一次多個查詢?是否有可能在同一個表中創建多發的MySQL查詢?

這裏是什麼,我試圖做一個例子。

我們有如下表:

| userid | price | stock | description | 
---------------------------------------- 
    1  10.00 5  some text 
    2  25.00 2  some text 
    3  15.00 3  some text 
    4  35.00 2  some text 
    5  30.00 4  some text 

,我試圖做的查詢是:

  1. MIN和MAX價格按說明
  2. 價格由用戶標識設置2
  3. 股票和價格的前三個結果只有沒有分組

所以HTML表看起來就像這樣:

description | Min_Price | Max_Price | Price Set by userid 2 | 1st Price | 1st Stock | 2nd Price | 2nd Stock | 3rd Price | 3rd Stock 
+0

是 - 工會,你也可以加入到同一個表或內嵌視圖... –

回答

0

該解決方案將需要許多「子查詢」或聯接。

你可能在尋找的東西,如:

select t1.description, 
     t1.min_price, 
     t1.max_price, 
     t2.user_id_2_price 
from 
    (select description, min(price) as min_price, max(price) as max_price from t group by description) t1 
left join 
    (select price, description as user_id_2_price from t where userid = '2') t2 
on 
    (t1.description = t2.description) 

,你可以添加儘可能多的這些「左連接」,因爲你需要

+0

運行此查詢我得到:在「關於條款」 –

+0

對不起......我忘了給一個別名到結果集 未知列「t2.description」,現在在代碼中的變化 – Michael

0

使用union,或有一排的一切,這樣的事情:

Select 
(Select min(price) from table) as min_price, 
(Select max(price) from table) as max_price, 
.... 
相關問題