Search code examples
mysqlsqlgroup-concat

Get list of values into one column through foreign key


I've two tables "sample" and "processes" and i want to get all the processes of given sample in one single column.

Table "sample" example:

----------------------------------------
|           Sample Table               |
|                                      |
|id  | timestamp  | other columns...   |
 --------------------------------------
|1   | 24/04/1994 | ...                |
|2   | 25/04/1994 | ...                |
|... |...         | ....               |  
----------------------------------------

Table "processes" example:

----------------------------------------
|          Processes Table             |
|                                      |
|id  | sample_id  | process_name       |
 --------------------------------------
|1   | 1          | facebook           |
|2   | 1          | tinder             |
|3   | 1          | clash royale       |
|4   | 2          | uno                |
|5   | 2          | whatsapp           |
|... |...         | ....               |  
----------------------------------------

Result:

------------------------------------------------------------
|          Result Table                                     |
|                                                           |
|sample_id  | timestamp  | processes                        |
 -----------------------------------------------------------
|1          | 24/04/1994 | [facebook, tinder, clash royale] |
|2          | 25/04/1994 | [uno, whatsapp]                  |
------------------------------------------------------------

Is it possible to this through SQL query? HOW?

P.S: I know that i can get all the processes of given sample with:

SELECT sample_id, timestamp, process_name
FROM sample, processes
WHERE sample.id = processes.sample_id

But this gives me too much unnecessary rows


Solution

  • You can try to use GROUP_CONCAT with CONCAT function.

    Schema (MySQL v5.7)

    CREATE TABLE sample(
       id INT,
       timestamp DATE
    
    );
    
    
    
    
    INSERT INTO sample VALUES (1,'1994/04/24'); 
    INSERT INTO sample VALUES (2,'1994/04/25'); 
    
    CREATE TABLE processes(
       id INT,
       sample_id INT,
      process_name VARCHAR(50)
    
    );
    
    
    INSERT INTO processes VALUES (1   ,1,'facebook');
    INSERT INTO processes VALUES (2   ,1,'tinder');
    INSERT INTO processes VALUES (3   ,1,'clash');
    INSERT INTO processes VALUES (4   ,2,'uno');
    INSERT INTO processes VALUES (5   ,2,'whatsapp');
    

    Query #1

    SELECT sample_id, timestamp, CONCAT('[',GROUP_CONCAT(process_name),']') processes
    FROM sample JOIN processes ON sample.id = processes.sample_id
    GROUP BY sample_id, timestamp;
    
    | sample_id | timestamp  | processes               |
    | --------- | ---------- | ----------------------- |
    | 1         | 1994-04-24 | [tinder,clash,facebook] |
    | 2         | 1994-04-25 | [uno,whatsapp]          |
    

    View on DB Fiddle

    NOTE

    I would use JOIN instead of use , because JOIN have emphasize relationship about connect two tables.