2014-01-07 34 views
0

好吧,我最終得到了一個max()正確拉動使用max() - 讓我有一段時間瞭解發生了什麼,並閱讀MySQL的限制使用它時,完成一個MAX(),但使用來自幾張表的數據

我花了一些時間做對sqlfiddle一些演示數據(一個下面是關於剛剛一個表

http://sqlfiddle.com/#!2/fff224/1

我想什麼(吸納爲香港專業教育學院最大試圖在最後2個小時得到這個工作)是如何合併另一個表,不需要使用最大值(我已經包括在這些sqlfiddle

結果我會後會

case_number full_address case owner client compiled date(max()) recommendation 
1000  1 high street  bob  london  14/12/2012     let 
1001  2 high street  ken  Compton  13/12/2013     sell 
1002  3 high street  ken  Leeds  14/12/2013     sell 

完成之間的內部連接,從客戶端\人員\到案件過程中我沒事其眼前這個嵌套選擇最大的IM翻倒在

+0

@Strawberry看起來很可能是'('1000','2012-12-14','let'),' –

+0

錯別字對不起,這是一個錯字,我已經糾正了結果 – Will

回答

1
SELECT p.case_number 
    , p.full_address 
    , s.case_owner_name 'case owner' 
    , c.client_name client 
    , r.compiled_date 
    , r.recommendation 
    FROM case_process p 
    JOIN staff s 
    ON s.case_owner_number = p.case_owner_number 
    JOIN client c 
    ON c.client_number = p.client_number 
    JOIN reporting r 
    ON r.case_number = p.case_number 
    JOIN (SELECT case_number,MAX(compiled_date) max_compiled_date FROM reporting GROUP BY case_number) x 
    ON x.case_number = r.case_number 
    AND x.max_compiled_date = r.compiled_date; 

+-------------+---------------+------------+---------+---------------+----------------+ 
| case_number | full_address | case owner | client | compiled_date | recommendation | 
+-------------+---------------+------------+---------+---------------+----------------+ 
|  1000 | 1 high street | Bob  | London | 2012-12-14 | let   | 
|  1001 | 2 high street | Ken  | Compton | 2013-12-13 | sell   | 
|  1002 | 3 high street | Ken  | Leeds | 2012-12-14 | sell   | 
+-------------+---------------+------------+---------+---------------+----------------+ 
3 rows in set (0.00 sec) 

fiddle of same... http://sqlfiddle.com/#!2/fff224/7 
+0

brill非常感謝! ! – Will

相關問題