tags:

views:

114

answers:

1

I need to alter my existing JOIN query below to also include the data from users.image correlating to the UserID of the post maker. Something like:

users.image WHERE users.UserID = posts.userid

I am not very good with join queries yet. How would I do this?

Existing Query:

$result = mysql_query("SELECT  posts.* FROM  listen JOIN posts ON posts.userid = listen.listenid WHERE listen.userid = '$user_id' ORDER BY DATE desc") or die(mysql_error());
+2  A: 

Just add another JOIN clause:

SELECT  posts.* 
FROM  listen 
JOIN posts ON (posts.userid = listen.listenid) 
JOIN users ON (users.UserID = posts.userid)
WHERE listen.userid = '$user_id' 
ORDER BY DATE desc

You may need to change the JOIN to a specific join such as LEFT JOIN, depending on what you're after.

Btw, it is easier to see the query on multiple lines.

Edit: You'll probably want to add additional items that you are selecting with your fields, such as SELECT posts.*, users.*

Kekoa
That makes sense an doesn't error but doesn't return the value I am trying to get.
ian
The query selects all the appropriate items but not the row of images I am trying to add... Is there something I need to do to select the matching users.image row and add it to the results?
ian
Currently it only selects posts.* but no field from `listen` or `users`. SELECT user.image, posts.* FROM ...
VolkerK