I have three tables for tagging. The first one is the question
table that has a list of question with certain ID. The second one is the tag
table that has a list of tag name with certain ID. And the third one is the question_tag
table, a collection of question to a tag. A question that has multiple tag means multiple rows in question_tag
, I thought of storing an serialized array into the question_tag
table but it's in general not a good idea to store array inside of a SQL database.
Below is the schema. Arrow denoting foreign key.
-------------------
----------------- | question_tag |
| question | ------------------- ------------------
----------------- | question_tag_ID | | tag |
| question_ID | ---> | question_ID | ------------------
----------------- | tag_ID | <----- | tag_ID |
------------------- | tag_name |
------------------
I want to make a query that will output this table below.
----------------------------------------------------
| question_id | tag_name |
----------------------------------------------------
| 1 | algebra, calculus, differentiation |
| 2 | calculus |
| 3 | algebra, trigonometry |
----------------------------------------------------
How do I manage to do this query? I thought about SELECTING from question
and JOINING
a temporary table of SELECT tag.tag_name FROM tag WHERE question_tag.tag_ID = tag.tag_ID
, but how do I output this RIGHT column (tag_name) like the table above.
I would really appreciate it if you can help me with this SQL query, I am guessing that I need to do a nesting SELECT query for the RIGHT (tag_name) column, then JOIN it to the question_table. But I am not sure how to the nesting of SELECT query.
This is what I have come up with:
SELECT * FROM question as Q LEFT JOIN (SELECT T.tag_name FROM tag as T WHERE T.tag_id IN (SELECT QT.tag_id FROM question_tag AS QT WHERE QT.question_ID = Q.id)) AS QT_T