Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- MySQl GROUP BY Issue


Spire2001 - 5:08 pm on Jul 28, 2008 (gmt 0)


Thanks for your reply.

Small error in my original example:

id ¦ file_name ¦ date_uploaded ¦ folder ¦ int_time
----------------------------------- --------------
1 ¦ name1.gif ¦ 281350Z Jul08 ¦ docs ¦ 0807281350
2 ¦ name1.gif ¦ 281353Z Jul08 ¦ docs ¦ 0807281353
3 ¦ name2.gif ¦ 281350Z Jul08 ¦ docs ¦ 0807281350
4 ¦ name2.gif ¦ 281355Z Jul08 ¦ docs ¦ 0807281355
5 ¦ name2.gif ¦ 281358Z Jul08 ¦ stuff ¦ 0807281358

"SELECT id, file_name, date_uploaded, folder, MAX(int_time) FROM tbl WHERE folder = 'docs' GROUP BY file_name";

Results returned are:

id ¦ file_name ¦ date_uploaded ¦ folder ¦ int_time
--------------------------------------------------
1 ¦ name1.gif ¦ 281350Z Jul08 ¦ docs ¦ 0807281353
3 ¦ name2.gif ¦ 281350Z Jul08 ¦ docs ¦ 0807281355

Note that the correct 'int_time' results are returned. They do not however correspond to the 'date_uploaded' field for the same row. data type of the 'int_time' field is INT(12).

Again, what I would like to have is a single row for each unique file name within each folder, and for said row to be of the latest addition. Such as:

id ¦ file_name ¦ date_uploaded ¦ folder ¦ int_time
----------------------------------- --------------
2 ¦ name1.gif ¦ 281353Z Jul08 ¦ docs ¦ 0807281353
4 ¦ name2.gif ¦ 281355Z Jul08 ¦ docs ¦ 0807281355

Each file name is returned once (per folder). Each row is the most recent, not the lowest 'id' #.


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/3709325.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com