开发者

MySQL: Group most up-to-date result with the same id_type only

I'm trying to make a SELECT with a table whic开发者_如何学编程h could contain more values with the same id_type like in this example:

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

How can I get the most up-to-date of every id_type? I thought something like this but dosen't work:

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

This to get a result like:

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


If i get your question correctly, this might work for what you want to do. You can now use the id column if that is the PK to join to the result of the below query.

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


I think you want to group by id rather than by id_type.

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


See this: http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜