MySQL split and join the values

后端 未结 2 830
心在旅途
心在旅途 2021-01-21 06:18

I have a table [mapping] with 2 columns similar to below

id  | values
1   | 1,2
2   | 1,2,3
3   | 1,1
4   | 1,1,2

and another table [map] is si

相关标签:
2条回答
  • 2021-01-21 06:42
    SELECT
        `ids`.`id`,
        GROUP_CONCAT(`values`.`texts`) AS texts
    FROM
        `ids`
    INNER JOIN `values` ON FIND_IN_SET(`values`.`id`, `ids`.`values`)
    GROUP BY
        `ids`.`id`
    

    It works like this: Example

    0 讨论(0)
  • 2021-01-21 06:44

    You can use MySQL FIND_IN_SET() to join the tables and GROUP_CONCAT() to concat the values :

    SELECT s.sno,GROUP_CONCAT(s.values) as `values` 
    FROM mapping t
    INNER JOIN map s ON(FIND_IN_SET(s.id,t.values))
    GROUP BY s.sno
    

    Note: You should know that this is a very bad DB structure. This may lead to a lot more complicated queries and will force you to over complicate things. You should Normalize your data, split it , and place each ID in a separate record!

    0 讨论(0)
提交回复
热议问题