I have figured out most of my SQL query, but I seem to need a little help.
I can’t seem to figure out how to select username from user by using social_posts.user_id.
Here’s my query (that works):
SELECT social_posts.user_id, social_posts.post_stamp, social_posts.message
FROM social_posts
WHERE id IN (
SELECT social_threads.id
FROM social_threads, friends
WHERE (((friends.user_a = ?) || (friends.user_b = ?)) && (friends.request = 1)) || (social_threads.user_id = ?)
ORDER BY social_threads.id DESC)
LIMIT 15
I would like the query to return user.username. In the user table, the user id is called id.
When I try the following I get an error:
SELECT social_posts.user_id, social_posts.post_stamp, social_posts.message, user.username
FROM social_posts
INNER JOIN user ON social_posts.user_id = user.id
WHERE id IN (
SELECT social_threads.id
FROM social_threads, friends
WHERE (((friends.user_a = ?) || (friends.user_b = ?)) && (friends.request = 1)) || (social_threads.user_id = ?)
ORDER BY social_threads.id DESC)
LIMIT 15
That query returns the following error:
Column 'id' in IN/ALL/ANY subquery is ambiguous
Although this works:
SELECT social_posts.user_id, social_posts.post_stamp, social_posts.message, user.username
FROM social_posts
INNER JOIN user ON social_posts.user_id = user.id
LIMIT 15
Any help is appreciated.
try this
I assume that
Order Byclause is with inner query,