2013-10-23 56 views
2

我有3個表:MySQL的最新帖子日期

member 
member_id|member_name 

training_member 
training_id|member_id 

training 
training_id|team_id|training_date 

我需要找到最新的培訓,TEAM_ID和training_date每個成員。 如果成員沒有得到任何培訓應該返回NULL作爲TEAM_ID和training_date

我已經嘗試了很多不同的方法,像這樣:

SELECT m.member_id, t.team_id, MAX(t.training_date) 
FROM member m 
JOIN training_member tm ON m.member_id = tm.member_id 
JOIN training t ON tm.training_id = t.training_id 
GROUP BY m.member_id 
ORDER BY m.member_name, t.training_date DESC 

它沒有給予正確的結果雖然,它似乎返回列出的第一個team_id,而不是與MAX關聯的那個(training_date)

我需要更改什麼?

編輯: sqlfiddle:http://sqlfiddle.com/#!2/a1ec1/1

+0

您可以發佈sqlfiddle? –

+0

當然,這裏你去: http://sqlfiddle.com/#!2/a1ec1/1 – Oxholm

回答

3

可能是這樣的: -

SELECT m.member_id, Sub2.team_id, Sub2.MaxTrainingDate 
FROM member m 
LEFT OUTER JOIN 
(
    SELECT tm.member_id, t.team_id, Sub1.MaxTrainingDate 
    FROM training_member tm 
    INNER JOIN training t ON tm.training_id = t.training_id 
    INNER JOIN 
    (
     SELECT tm.member_id, MAX(t.training_date) AS MaxTrainingDate 
     FROM training_member tm 
     LEFT OUTER JOIN training t ON tm.training_id = t.training_id 
     GROUP BY tm.member_id 
    ) Sub1 
    ON Sub1.member_id = tm.member_id 
    AND Sub1.MaxTrainingDate = t.training_date 
) Sub2 
ON Sub2.member_id = m.member_id 
ORDER BY m.member_name, MaxTrainingDate DESC 

使用一個內部的選擇,讓每件最新的培訓,加入,對訓練員和訓練表,以獲取其他細節(即team_id),然後LEFT與成員加入(應對未經過培訓的成員)。

編輯 - 刪除子查詢並將它們放入視圖以允許主SQL成爲視圖。

查找來獲得最大的訓練日期爲每個成員

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vw_max_training` AS select `tm`.`member_id` AS `member_id`,max(`t`.`training_date`) AS `MaxTrainingDate` from (`training_member` `tm` left join `training` `t` on((`tm`.`training_id` = `t`.`training_id`))) group by `tm`.`member_id`; 

查找來獲得球隊細節每個成員最大的訓練日期

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vw_max_training_team` AS select `tm`.`member_id` AS `member_id`,`t`.`team_id` AS `team_id`,`sub1`.`MaxTrainingDate` AS `MaxTrainingDate` from ((`training_member` `tm` join `training` `t` on((`tm`.`training_id` = `t`.`training_id`))) join `vw_max_training` `sub1` on(((`sub1`.`member_id` = `tm`.`member_id`) and (`sub1`.`MaxTrainingDate` = `t`.`training_date`)))); 

最終視圖

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vw_main` AS select `m`.`member_id` AS `member_id`,`sub2`.`team_id` AS `team_id`,`sub2`.`MaxTrainingDate` AS `MaxTrainingDate` from (`member` `m` left join `vw_max_training_team` `sub2` on((`sub2`.`member_id` = `m`.`member_id`))) order by `m`.`member_name`,`sub2`.`MaxTrainingDate` desc; 
+0

這給出了正確的結果! 但是...因爲我需要在視圖中使用它,所以我得到:「視圖的SELECT在FROM子句中包含子查詢」 – Oxholm

+0

嘗試將每個子查詢轉換爲獨立視圖,然後使用選擇中的視圖代替。將編輯我的答案爲例。 – Kickstart

+0

非常感謝!你剛剛救了我的一天,它工作完美! – Oxholm

0

檢查此

0

使用LEFT JOIN返回null如果該成員沒有任何培訓。

下面是代碼:

SELECT m.member_id, t.team_id, max(t.training_date) 
FROM member m 
LEFT JOIN training_member tm ON m.member_id = tm.member_id 
LEFT JOIN training t ON tm.training_id = t.team_id 
GROUP BY tm.member_id 
ORDER BY m.member_name, t.training_date DESC 

演示:http://sqlfiddle.com/#!2/a1ec1/42

+0

對不起,這並沒有給出正確的結果,請參閱新的小提琴: http://sqlfiddle.com/#!2/e9e747/2 – Oxholm

+0

它將除非,如果表training_member的任何'training_id'是不存在於表'訓練'的'team_id'中。在上面的sql小提琴中,您添加了'training_id' 7,它不存在於'training'表的'team_id'中,所以它返回了意想不到的結果。 –

+0

這裏的一個問題是,t.team_id的值可能來自m.memberid所做的任何訓練,而不一定是對應於最大訓練日期的訓練。 – Kickstart