MySQL query: join these tables in order to get all tag_names used by any given `user_id` on all of his images?
Consider following tables structure:
users (user_id, ...)
images (image_id, user_id, ...)
tag_links (tag_id, image_id)
tags_names (tag_id, tag_name)
Let's say any given user_id
has multiple images in the images
table, and an image has multiple tags stored in tag_links
.
How can I join these tables in order to get all tag_names used by any given user_id
on all of his images? Are these kind of queries (joing multiple tables) slow and n开发者_如何学Pythonot recommended, or is it common to do that?
Joining is common and it's the way it should be done.
SELECT tn.tag_name
FROM tags_names AS tn
INNER JOIN tag_links AS tl ON tn.tag_id=tl.tag_id
INNER JOIN images AS i ON tl.image_id=i.image_id
WHERE i.user_id=YOURUSERID
Your table design though only makes sense if a tag can have more then one name, if not use a column name
on tag_links
.
精彩评论