Jump to content

Join Query Help


freshwebs

Recommended Posts

This is probably a really simple query, but I'm having trouble figuring it out.

 

I have two tables.  One has user account information and the second has profile information.  Changes to the profile are logged, so every time someone saves their profile, another row is added to the profiles table with the profile data, user_id, and date.

 

I'm trying to write a query to join the two tables, only using the most recent profile data for each user.  So, the query will return one row for every user with all their info.  How would this be done?

 

Thanks in advance!

Link to comment
Share on other sites

Users: id, first_name, last_name, email, password

Profiles: id, user_id, date, address, city, state, zip, etc.

 

Every time the user changes their profile, another row is added with their user_id and the date.  The query needs to select their row from the users table and their latest profile record.

 

Thanks!

Link to comment
Share on other sites

Give this a try. Remember to set $userID to the userID you're trying to request.

 

SELECT u.first_name, u.last_name, u.email, u.password, p.address, p.city, p.state, p.zip FROM tblUser u, tblProfile p WHERE u.id = $userID AND u.id = p.user_id ORDER BY p.date DESC LIMIT 1

Link to comment
Share on other sites

Ah ok. Then try this one...

 

SELECT u.first_name, u.last_name, u.email, u.password, p.address, p.city, p.state, p.zip FROM tblUser u, tblProfile p WHERE u.id = p.user_id  AND p.date = (SELECT MAX(p2.date) FROM tblProfile p2 WHERE p2.user_id = u.id) ORDER BY p.date DESC LIMIT 1

 

There is probably a better way to do this but without actually being able to test it this is all I can provide. This will return all user/profile relations and then discard all but the one that has the highest date (ie the most recent one).

 

Can I ask, how come you create a new profile each time the user updates it? It seems highly inefficient to me. I imagine you have a reason for keeping their old details on file?

Link to comment
Share on other sites

Thanks, that gets the job done.  I think there might be a more efficient way to do it, but it's sufficient for now.

 

This is a really dumbed down version of what I'm doing.  Basically, we need to be able view what someone's profile looked like any given week.  I have it setup so that at max one new profile record is created each week, otherwise it's just updated.

 

Let me know if you, or anyone else, has any improvements on the last query.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.