Jump to content

Getting what I need out of a query...


ShootingBlanks

Recommended Posts

Hello.  I have data in a "projects" table with "proj_id" (primary key) and "proj_name" columns. I also have a lookup table called "update_progress" with three columns:

 

* proj_id (which relates to the proj_id primary key in the "projects" table)

* updated (a TIMESTAMP that auto-updates the current timestamp on update)

* progress (a text field)

 

Now, I need to display each project title (from the "projects" table) one time, and then I need to display only the most RECENT progress that relates to it (from the new "update_progress" table) after that. This is where I'm having problems...

 

I have it so that it's displaying EVERY update (not just the most recent one). So if there's three updates, the project will list three times - once for each update...

 

Here's the code that's doing that - please let me know how to fix it. Thanks!:

SELECT projects.proj_id, projects.proj_name,
CONCAT(DATE_FORMAT(update_progress.updated, '%Y-%m-%d'),': ',update_progress.progress) AS progress
FROM projects, update_progress
WHERE projects.proj_id = update_progress.proj_id

Link to comment
Share on other sites

Untested:

 

SELECT p.proj_id, p.proj_name, CONCAT(DATE_FORMAT(up.updated, '%Y-%m-%d'),': ',up.progress) AS progress
INNER JOIN update_progress AS up ON ( up.proj_id = p.proj_id )
INNER JOIN 
( SELECT proj_id, MAX(updated) AS maxUpdated
FROM update_progress
GROUP BY proj_id ) AS sub ON ( sub.proj_id = up.proj_id AND sub.maxUpdated = up.updated  )
[code]

[/code]

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.