我有一個整數的表(I)(0-9)(我也是)...
我沒有認真提倡這是一個解決方案,但只是爲了好玩。 ..
DROP TABLE IF EXISTS table_a;
DROP TABLE IF EXISTS table_b;
CREATE TABLE table_a
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,users VARCHAR(100) NULL
);
INSERT INTO table_a VALUES
(1,'["2","5","4"]'),
(2,NULL),
(3,'["5"]');
CREATE TABLE table_b
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
);
INSERT INTO table_b VALUES
(2,'Jane'),
(3,'Joe'),
(4,'John'),
(5,'Jack');
SELECT b.name
, GROUP_CONCAT(a.id ORDER BY a.id+0) ids
FROM table_b b
LEFT
JOIN
(SELECT DISTINCT a.id, SUBSTRING_INDEX(SUBSTRING_INDEX(
REPLACE(
REPLACE(
REPLACE(a.users,'"','')
,'[','')
,']','')
,',',i.i+1)
,',',-1)
x
FROM
table_a a,ints i
) a
ON a.x = b.id
GROUP
BY name;
+------+------+
| name | ids |
+------+------+
| Jack | 1,3 |
| Jane | 1 |
| Joe | NULL |
| John | 1 |
+------+------+
您的'用戶'列是否真的包含字符串'[「2」,「5」,「4」]? –
不知道該怎麼稱呼它,但JSON_ENCODE的結果 –
如果我去那裏,我不會從這裏開始:-(。json_encode使這個不必要的複雜 – Strawberry