John
John

Reputation: 171

Select from three tables and show the results

I'm trying to follow tutorials for select and joining multiple tables but can't understand it yet. So I have tables: images, favorites, users.

images - image_id, image_name ... etc

users - id, name ... etc

favorites - image_id, id

In table favorites, image_id represent id of the image and id is id of the user. On the image there is button Add to favorite. Now I want to display all the images which user added to his collection of favorites.

This is what I tried but doesn't work

SELECT f.image_id, f.id
FROM favorites
JOIN users u ON f.id = u.id
JOIN images i ON f.image_id = i.image_id
WHERE u.id = 2

1054 - Unknown column 'f.image_id' in 'field list'

Upvotes: 0

Views: 25

Answers (1)

Disha V.
Disha V.

Reputation: 1864

You missed alias f in the query

SELECT f.image_id, f.id
FROM favorites f
JOIN users u ON f.id = u.id
JOIN images i ON f.image_id = i.image_id
WHERE u.id = 2

Upvotes: 1

Related Questions