简体   繁体   中英

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM