The objective is a little bit difficult to explain, so please, notify me if you need more information.
I’m creating a blog, where people can post articles, but also promote posts of other users. The user posts and promoted posts will appear in its profile page.
To give you more information, this thing was already done in tumblr, or twitter (with retweet): In user profile, we have its tweets, but also tweets that he retweeted.
Here are the database tables:
Table “Posts”:
- id
- author_id
- title
- content
- created
And the “Promotions” table (person X promotes the post Y):
- id
- promoter_id (the user id that promoted the post Y: Mr X)
- post_id (the promoted post: Y)
- created
Given a user id, I want to retrieve posts that he wrote and the posts that he promoted. The question is: what is the SQL (specially MySQL) query?
And order the list according on “created” field.
Thanks in advance.
OK, I’ve got it. Here is the answer:
So, the magic trick happens when mixing LEFT JOIN and the OR condition.
Hope it will help.
PS. Thanks for all participants.