Jump to content

Querying from Multiple Tables


cussbee

Recommended Posts

Here's my prob, finally got the first part working but I need to add in another table to this query. The query below is on the "messages" table that stores all the different messages in a threaded system and grabs the latest message in a thread.

 

SELECT * FROM (SELECT thread, MAX(id) AS id FROM messages GROUP BY thread) t1 INNER JOIN messages t2 ON t2.thread=t1.thread AND t1.id=t2.id WHERE tome = '$userid' AND INDELETE !='y' ORDER BY t1.id DESC

 

My problem is, I need to pull out the fields "firstname", "lastname" and "image1" of the user sending the message to the person from the table "users" where the field "fromme" in the messages table is = to the "id" field in the users table. How would I got about changing up the query to make this go? Thanks!

Link to comment
Share on other sites

I am not great at knowing which type of join to use, but this should work:

 

SELECT * FROM 
(SELECT thread, MAX(id) AS id FROM messages GROUP BY thread) t1 
INNER JOIN messages t2 ON t2.thread=t1.thread AND t1.id=t2.id 
JOIN users t3 ON t2.fromme = t3.id
WHERE tome = '$userid' AND INDELETE !='y' ORDER BY t1.id DESC

 

See if that does what you want it to.

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.