Let's say I have 3 main tables:
category
, tag
, url
category
and tag
have a MANY_MANY relationship (category_tag
).
tag
and url
have a MANY_MANY relationship (url_tag
).
I would like for a given category to retrieve all the linked urls.
What is the easiest, most efficient way to do this?
Could I maybe build a "VIEW"? What would be the query?
I am using MySQL.
Best Answer
Encapsulating the queries into Views or not, is your choice. For every one of the below queries, you can create a view:
If you want only the IDs of the categories and URLs, the simplest (joining only 2 tables) is this - no need for joining the
tag
table, as long as the foreign key constraints are defined:or (the equivalent) which can be easily extended to include the number of tags relating a category with a url:
If you also want columns from the
category
andurl
tables as well (which is very probable), you need to join those tables, too: