user1174762 user1174762 - 6 months ago 8
MySQL Question

Join two tables in a MySQL query

I am new to PHP and MySQL and I am having trouble understanding

JOIN
. I think, for me, the problem lies with actually understanding the logic of the query. What I am trying to do is select all of the status updates from a table named "post", but only the ones from users I am "following", and then display them in order by date. So, I have two databases which are set up like this:

posts

|post_id|user_id|post_body|date_upload|
| 1 | 4 | hey. | 01/2/2012 |


follows

|relation_id|user_id|followee_id|
| 1 | 4 | 2 |


Could someone please explain how I should syntactically and logically set this up?

Thank you!

Eva Eva
Answer

I think you'll find this will work:

SELECT
  p.post_id, 
  p.user_id,
  p.post_body,
  p.date_upload
FROM
  posts p,
  follows f
WHERE
  f.user_id = 4 
    AND
  f.followee_id = p.user_id
ORDER BY
  p.date_upload DESC
Comments