各位前輩好,
超級菜鳥我又卡關了...TAT,
資料庫的table欄位中有json資料,
上網爬文說可以用split切割出來但一直嘗試失敗,
跪求高手們出手相救...(╥﹏╥)。
原本的table:
month | json
1 | [" 99/01/04", " 99/01/05", " 99/01/06", " 99/01/07"]
2 | [" 99/02/04", " 99/02/05", " 99/02/06", " 99/02/07"]
3 | [" 99/03/04", " 99/03/05", " 99/03/06", " 99/03/07"]
目標的table:
month | date
1 | 99/01/04
1 | 99/01/05
1 | 99/01/06
1 | 99/01/07
2 | 99/02/04
2 | 99/02/05
2 | 99/02/06
2 | 99/02/07
3 | 99/03/04
3 | 99/03/05
3 | 99/03/06
3 | 99/03/07
範例網址:
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c03ca4cc6651415d1069b2a1cd5322b9
範例網址(更新):
https://dbfiddle.uk/I6qunLKC
可以嘗試使用 JSON_EXTRACT
與 JSON_LENGTH
達到你要的結果
範例:
https://stackoverflow.com/a/51035417/9151543
https://stackoverflow.com/a/54673918/9151543
參考這篇
改出這個 db fiddle
看看合不合用
CREATE TABLE TableDate (
month INT(20),
name VARCHAR(100));
INSERT INTO TableDate VALUES
('01','[" 99/01/04", " 99/01/05", " 99/01/06", " 99/01/07"]'),
('02','[" 99/02/04", " 99/02/05", " 99/02/06", " 99/02/07"]'),
('03','[" 99/03/04", " 99/03/05", " 99/03/06", " 99/03/07"]');
CREATE TABLE numbers (
n INT(20) PRIMARY KEY);
INSERT INTO numbers VALUES
('01'),('02'),('03'),('04'),('05'),('06'),
('07'),('08'),('09'),('10'),('11'),('12');
SELECT
TableDate.month,
REPLACE(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(TableDate.name, '", " ', numbers.n), '", " ', -1),'[" ',''),'"]','') date
FROM
numbers INNER JOIN TableDate
ON CHAR_LENGTH(TableDate.name)
-CHAR_LENGTH(REPLACE(TableDate.name, ',', ''))>=numbers.n-1
ORDER BY
month, n
CREATE TABLE TableDate(
month int(20),
json varchar(500)
)
INSERT TableDate (month,json)
VALUES
('01','[" 99/01/04", " 99/01/05", " 99/01/06", " 99/01/07"]'),
('02','[" 99/02/04", " 99/02/05", " 99/02/06", " 99/02/07"]'),
('03','[" 99/03/04", " 99/03/05", " 99/03/06", " 99/03/07"]')
資料格式很整齊,就一招
SELECT month,SUBSTRING(json,4,8) AS F1
FROM TableDate
UNION ALL
SELECT month,SUBSTRING(json,17,8) AS F1
FROM TableDate
UNION ALL
SELECT month,SUBSTRING(json,30,8) AS F1
FROM TableDate
UNION ALL
SELECT month,SUBSTRING(json,43,8) AS F1
FROM TableDate
ORDER BY month,F1
create table it1210 (
mon int
, js json
);
-- 宣告為 json 型態.
-- 原發問者是用 varchar(500)
insert into it1210 values
('01','[" 99/01/04", " 99/01/05", " 99/01/06", " 99/01/07"]'),
('02','[" 99/02/04", " 99/02/05", " 99/02/06", " 99/02/07"]'),
('03','[" 99/03/04", " 99/03/05", " 99/03/06", " 99/03/07"]');
-- 多增加一筆,資料長度不同的.
insert into it1210 values
('04','[" 99/04/04", " 99/04/05", " 99/04/06", " 99/04/07", " 99/04/08"]');
select i.mon
, json_extract(js, concat('$[', c.n, ']')) as dt
from (select @n := @n + 1 as n
from (select @n := -1) a
, (select 1
from information_schema.CHARACTER_SETS
limit 10) b) c
join it1210 i
where c.n < json_length(js)
order by 1,2;
+------+-------------+
| mon | dt |
+------+-------------+
| 1 | " 99/01/04" |
| 1 | " 99/01/05" |
| 1 | " 99/01/06" |
| 1 | " 99/01/07" |
| 2 | " 99/02/04" |
| 2 | " 99/02/05" |
| 2 | " 99/02/06" |
| 2 | " 99/02/07" |
| 3 | " 99/03/04" |
| 3 | " 99/03/05" |
| 3 | " 99/03/06" |
| 3 | " 99/03/07" |
| 4 | " 99/04/04" |
| 4 | " 99/04/05" |
| 4 | " 99/04/06" |
| 4 | " 99/04/07" |
| 4 | " 99/04/08" |
+------+-------------+
8版的方法
select i.mon
, r.res
from it1210 i
, json_table(js, '$[*]' columns (res text path '$')) r
order by 1, 2;