I feel like there is a better way to do this. I'm mostly just asking if what I'm doing is right, or if there's a much better way.
Observe these tables:
Users
id | name
---+---------
1 | Andrew
2 | Greg
Images
id | userid | url
---+--------+-------
1 | 1 | img/foo.jpg
2 | 2 | img/bar.jpg
3 | 2 | img/baz.jpg
4 | 1 | img/qux.jpg
If the Users table had a single foreign key that references Images, each user could only be associated with a single image. However, I'd like to allow each user to be associated with any number of images, hence putting a Users foreign key in Images instead.
I understand how to use JOIN if user has a single foreign key to data stored in another table. However, in this case, there is another table with a number of foreign keys associated with the user I'm interested in. How would I construct a SELECT query that will retrieve all of the images associated with the user in addition to all of the data associated with the user (user type, phone number, blah blah blah [I know how to do this already]) in one SELECT query?
Or, is there a better way to do this?
Edit 1:
For example, if you select the user with id = 1 then it would return the first row of Users and the first and last row of Images (because those rows have userid = 1).