SQL Troubles with JOIN, GROUP BY and MAX???
| From: | Michael Robbins | Date: | Tue, 04 Jun 2002 04:19:16 +0000 |
| Subject: | SQL Troubles with JOIN, GROUP BY and MAX??? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-19591@lists.php.net to get a copy of this message | ||
I've been racking my brain for too long on this one, can someone help me
out with a SQL query?
I've got 2 tables in a mySQL database, 'users' and 'files' defined as
such:
USERS
-------------------
Id int (autoincrement)
Username varchar
FILES
-------------------
Id int (autoincrement)
Userid int
Stamp int (holds unix_timestamp for
filectime($filename))
Filename varchar
What I want is a single query that returns all of the users that have
submitted files, and only the MOST RECENT file for each user. I'm sure
I need a GROUP BY and/or a MAX() in there somewhere.....
Maybe if I LEFT JOIN the table to itself........
Right now, I do one query for a list of users with files, then a
separate query for each user to find their most recent file. It now
takes in excess of 100 queries to build the page!
There must be a better way.
Thanks in advance for your help.
Mike