相关国家列表

A list with correlated countries

我有两张桌子 第一个类似于:

    | id | id_related     | 
    | id1| [id2, id3, id4]|
    | id2| [id1, id4]     |
    | id3| [id1, id2, id5]|

第二个类似于:

    |id | country |
    |id1| BR      |
    |id2| US      |
    |id3| CO      |
    |id4| IT      |
    |id5| US      |

我需要一个 return:

的查询

   | id | id_related     | related_country |
   | id1| [id2, id3, id4]| [US, CO, IT]    |
   | id2| [id1, id4]     | [BR, IT]        |
   | id3| [id1, id2, id5]| [BR, US, US]    |

我试图用 select 和 concat 做一个 select,但没有任何效果。有人可以帮助我吗?

你的数据

CREATE TABLE mytable1(
   id         VARCHAR(100) NOT NULL 
  ,id_related VARCHAR(100) NOT NULL

);
INSERT INTO mytable1
(id,id_related) VALUES 
('id1','[id2,id3,id4]'),
('id2','[id1,id4]'),
('id3','[id1,id2,id5]');

CREATE TABLE mytable2(
   id      VARCHAR(100) NOT NULL 
  ,country VARCHAR(100) NOT NULL
);
INSERT INTO mytable2(id,country) VALUES 
('id1','BR'),  
('id2','US'),
('id3','CO'),
('id4','IT'),
('id5','US');

首先,您应该使用以下查询删除 [ ] 个字符

SELECT id,
       Trim(BOTH '[' FROM Trim(BOTH ']' FROM id_related)) AS id_related
FROM   table1  

输出结果

id id_related
id1 id2,id3,id4
id2 id1,id4
id3 id1,id2,id5

那么您应该使用 in this link 的方法将值拆分为多行

SELECT t12.id,
       t2.country
FROM   (SELECT t11.id,
               Substring_index(Substring_index(t11.id_related, ',', numbers.n),
               ',', -1
                      )
               id_related
        FROM   (SELECT 1 n
                UNION ALL
                SELECT 2
                UNION ALL
                SELECT 3
                UNION ALL
                SELECT 4
                UNION ALL
                SELECT 5) numbers
               INNER JOIN (SELECT id,
                                  Trim(BOTH '[' FROM Trim(BOTH ']' FROM
                                                     id_related)) AS
                                                     id_related
                           FROM   table1) t11
                       ON Char_length(t11.id_related) - Char_length(
                          REPLACE(t11.id_related, ','
                          , '')) >=
                          numbers.n - 1) t12
       JOIN table2 t2
         ON t2.id = t12.id_related  

得到以下结果

id id_related
id3 id1
id2 id1
id1 id2
id3 id2
id2 id4
id1 id3
id3 id5
id1 id4

然后使用GROUP_CONCATConcatmultiple joinSubquery得到你想要的结果如下

SELECT t12.id,
       t13.id_related,
       Concat('[', Group_concat(t2.country ORDER BY t12.id_related ASC), ']') AS
       related_country
FROM   (SELECT t11.id,
               Substring_index(Substring_index(t11.id_related, ',', numbers.n),
               ',', -1
                      )
               id_related
        FROM   (SELECT 1 n
                UNION ALL
                SELECT 2
                UNION ALL
                SELECT 3
                UNION ALL
                SELECT 4
                UNION ALL
                SELECT 5) numbers
               INNER JOIN (SELECT id,
                                  Trim(BOTH '[' FROM Trim(BOTH ']' FROM
                                                     id_related)) AS
                                                     id_related
                           FROM   table1) t11
                       ON Char_length(t11.id_related) - Char_length(
                          REPLACE(t11.id_related, ','
                          , '')) >=
                          numbers.n - 1) t12
       JOIN table2 t2
         ON t2.id = t12.id_related
       JOIN table1 t13
         ON t13.id = t12.id
GROUP  BY t12.id,
          t13.id_related  

dbfiddle