2010-11-07 51 views
0

我試圖與其中可能包含具有相同id_type更多的價值就像這個例子表中的SELECTMySQL的:集團最先進的最新結果與同ID_TYPE唯一

table_intensity 
id  id_type  value  user_id  creation_date 
1  1    90.4  73    2010-11-06 16:27:32 
2  4    1258  27    2010-11-06 16:27:48 <= up-to-date id_type 
3  3    1.9  73    2010-11-06 16:31:02 <= up-to-date id_type 
4  1    88.8  12    2010-11-06 16:33:11 
5  1    89.1  12    2010-11-06 16:58:20 <= up-to-date id_type 

我怎樣才能獲得每個id_type的最新信息?我想是這樣的,但這麼想的工作:

SELECT * FROM table_intensity GROUP BY id_type ORDER BY id_type, creation_date 

這要得到這樣的結果:

table_intensity 
id  id_type  value  user_id  creation_date 
2  4    1258  27    2010-11-06 16:27:48 
3  3    1.9  73    2010-11-06 16:31:02 
5  1    89.1  12    2010-11-06 16:58:20 
+0

我猜你還需要ORDER BY CREATION_DATE DESC – kalpaitch 2010-11-07 00:29:16

回答

0

如果我正確地得到您的問題,這可能適用於您想要做的事。 現在可以使用id列,如果這是要加入到下面的查詢結果的PK。

SELECT * 
FROM table_intensity A 
INNER JOIN 
    (SELECT id_type, MAX(creation_date) FROM table_intensity GROUP BY id_type) b 
ON A.id_type = b.id_type 
AND A.creation_date = b.creation_date 
0

我想你想按id而不是id_type

SELECT * FROM table_intensity GROUP BY id ORDER BY id_type, creation_date