Welcome to WebmasterWorld Guest from 54.145.11.9

Forum Moderators: open

Message Too Old, No Replies

Adding a Second Table into Query

MySQL

     
11:24 pm on Dec 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Dec 23, 2009
posts: 1
votes: 0


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

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. Any ideas? THANKS! JC

11:54 pm on Jan 4, 2010 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
posts:3416
votes: 0


Welcome to WebmasterWorld, cussbee!

Maybe something like this would do the trick?

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

Kind of hard to tell because I'm not really sure what's going on with the current temporary table and inner join. But, hopefully this will point you in a direction if you haven't already sorted this out. :)