I’ve been working on some MySQL Joins but this one has got me a little confused.
I have 3 tables, friends, posts and activity.
Tables posts and activity both have a field userid which represents the user that the record belongs to. friends contains the fields userid and friendid to represent two users being friends.
If possible, I would like to design a query which will display the records in posts AND activity from a user which is friends with the logged in user.
I have tried the following query:
SELECT p.*, a.*
FROM posts AS p, activity AS a
INNER JOIN friends f ON (p.userid = f.friendid)
WHERE f.userid = '(Logged In User ID)'
However, when I run the above query, I get the following error:
Unknown column 'p.userid' in 'on clause'
Any help would be much appreciated 🙂
You probably need a
UNION. The following will work if the two tables,postsandactivityhave the same number of columns (in same order and similar type). If not, some adjustment is needed in theSELECTlists:If the two tables have different fields, then you should adjust the 2 select lists, with something like: