Question

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

Was it helpful?

Solution

You'll need to aggregate and concatenate. Please check out some of the related questions:

Does T-SQL have an aggregate function to concatenate strings?

Implode type function in SQL Server 2000?

Concatenate row values T-SQL

How to use GROUP BY to concatenate strings in MySQL?

Aggregate String Concatenation in Oracle 10g

Create a delimitted string from a query in DB2

How to concatenate strings of a string field in a PostgreSQL 'group by' query?

UPDATE

While I don't have a mysql DB to test this on, going off of one of the above links (with the knowledge this is mysql), I've designed the following query:

SELECT 
    qt.question_id, 
    GROUP_CONCAT(t.tag_name SEPARATOR ',') 
FROM question_tag qt
LEFT JOIN tag t ON t.tag_id = qt.tag_id
GROUP BY qt.question_id;

Please try this out and let me know if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top