Mysql comma separated value

Raja Dhasan

I have two tables which looks similar to below table 1 & Table 2.I want out put similar to table 3

  Table 1

ParticipantsId | Description | Duration
1,2                 Demo        60 mins
1                   Test        25 mins
1,2,3,4             Alpha       30 mins
4,5,6,2             MCQ         120 mins

Table 2

UserId |  Name   |  Age
  1       Aku        21
  2       Greg       18
  3       Denver     24
  4       Mike       22
  5       Sid        24
  6       Sriten     19 

I want the out put to be

Table 3

users                   | Description | Duration
Aku,Greg                   Demo          60
Aku                        Test          25
Aku,Greg,Denver,Mike       Alpha         30
Mike,Sid,Sriten,Greg       MCQ           120  

I have tried find_by_set , group and various other possibilities but I am not able to get the required results, Please help

mitkosoft

You can use GROUP_CONCAT() and FIND_IN_SET() together:

SELECT
    GROUP_CONCAT(t2.`name`) AS users,
    t1.Description,
    CAST(t1.Duration AS UNSIGNED) AS Duration
FROM
    table1 t1,
    table2 t2
WHERE
    FIND_IN_SET(t2.userID, t1.ParticipantsId)
GROUP BY
    t1.Description,
    CAST(t1.Duration AS UNSIGNED)

result is:

+----------------------+-------------+----------+
| users                | Description | Duration |
+----------------------+-------------+----------+
| Greg,Denver,Aku,Mike | Alpha       |       30 |
| Greg,Aku             | Demo        |       60 |
| Greg,Mike,Sid,Sriten | MCQ         |      120 |
| Aku                  | Test        |       25 |
+----------------------+-------------+----------+
4 rows in set

However you must think about normalizing of data structure, comma separated values is a bad practice as someone mentions before.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check with a value in comma separated values in MySQL?

From Dev

How to query a string value separated by comma in MySQL?

From Dev

Need most popular value in mysql comma separated value?

From Dev

mysql join get column value from comma separated value

From Dev

MySQL comma separated strings

From Dev

MySQL query get value comma separated from master detail table

From Dev

To fetch value with comma separated ID with Join two tables in MySQL

From Dev

MySQL Splitting string value separated by comma using INSERT INTO/SELECT

From Dev

Convert Comma Separated Value to Dictionary

From Dev

Column to comma separated value in Hive

From Dev

Ant LineContains comma separated Value

From Dev

Sql Compare Comma Separated Value

From Dev

Convert Comma Separated Value to Dictionary

From Dev

ucfirst after comma separated value

From Dev

Split a Comma Separated Value of a Field

From Dev

Mapper Output as Comma Separated Value

From Dev

Filter MySQL comma separated field

From Dev

REGEX mysql for comma separated values

From Dev

Update comma separated list in mysql

From Dev

Searching on a comma separated mysql column

From Dev

REGEX mysql for comma separated values

From Dev

Splitting a comma separated string MySql

From Dev

Searching on a comma separated mysql column

From Dev

MySQL: comma separated string within IN()

From Dev

Intersect comma separated lists in mysql

From Dev

If decimal value, convert to two decimals AND dot separated value to comma separated

From Dev

MySQL: Insert multiple rows in database based on comma separated value via PHP

From Dev

How can I use comma separated string from a column as value for MySQL IN statement

From Dev

How to get Concat comma separated value from mysql stored procedure or function?

Related Related

  1. 1

    How to check with a value in comma separated values in MySQL?

  2. 2

    How to query a string value separated by comma in MySQL?

  3. 3

    Need most popular value in mysql comma separated value?

  4. 4

    mysql join get column value from comma separated value

  5. 5

    MySQL comma separated strings

  6. 6

    MySQL query get value comma separated from master detail table

  7. 7

    To fetch value with comma separated ID with Join two tables in MySQL

  8. 8

    MySQL Splitting string value separated by comma using INSERT INTO/SELECT

  9. 9

    Convert Comma Separated Value to Dictionary

  10. 10

    Column to comma separated value in Hive

  11. 11

    Ant LineContains comma separated Value

  12. 12

    Sql Compare Comma Separated Value

  13. 13

    Convert Comma Separated Value to Dictionary

  14. 14

    ucfirst after comma separated value

  15. 15

    Split a Comma Separated Value of a Field

  16. 16

    Mapper Output as Comma Separated Value

  17. 17

    Filter MySQL comma separated field

  18. 18

    REGEX mysql for comma separated values

  19. 19

    Update comma separated list in mysql

  20. 20

    Searching on a comma separated mysql column

  21. 21

    REGEX mysql for comma separated values

  22. 22

    Splitting a comma separated string MySql

  23. 23

    Searching on a comma separated mysql column

  24. 24

    MySQL: comma separated string within IN()

  25. 25

    Intersect comma separated lists in mysql

  26. 26

    If decimal value, convert to two decimals AND dot separated value to comma separated

  27. 27

    MySQL: Insert multiple rows in database based on comma separated value via PHP

  28. 28

    How can I use comma separated string from a column as value for MySQL IN statement

  29. 29

    How to get Concat comma separated value from mysql stored procedure or function?

HotTag

Archive