Я попытался
mysqli_insert_id ($ dbConnectionObj)
Это возвращает последний вставленный идентификатор текущего соединения, поэтому, если вы правильно управляете своими подключениями, это должно работать , По крайней мере, работал для меня.
Вы можете извлечь столбцы, выполнив:
select substring_index(column, ',', 1) as data
from t
union all
select substring_index(substring_index(column, ',', 2), -1) as data
from t
where column like '%,%'
union all
select substring_index(substring_index(column, ',', 3), -1) as data
from t
where column like '%,%,%'
union all
select substring_index(substring_index(column, ',', 4), -1) as data
from t
where column like '%,%,%,%';
Получить идентификатор ребенка сложно Вы можете сделать это, используя исходный столбец и некоторые переменные:
select c.col_id, t.data
from (select column, substring_index(column, ',', 1) as data
from t
union all
select column, substring_index(substring_index(column, ',', 2), -1) as data
from t
where column like '%,%'
union all
select column, substring_index(substring_index(column, ',', 3), -1) as data
from t
where column like '%,%,%'
union all
select column, substring_index(substring_index(column, ',', 4), -1) as data
from t
where column like '%,%,%,%'
) t join
(select column, (@rn := @rn + 1) as col_id
from t cross join
(select @rn := 0) params
) c
on t.column = c.column;
Предположим, это ваша таблица:
create table old_table(
data text
);
insert into old_table(data)values
("['ffffffff-11111-1111-baaa-xxxx']"),
("['zxyvvv-1234567-abcdefghijk', '1234567-abcdefg-hijklmn']");
И вы хотите «перенести» данные в две новые таблицы:
create table parent_table(
parent_id int primary key
);
create table child_table(
child_id int auto_increment primary key,
parent_id int not null,
data varchar(100),
foreign key (parent_id) references parent_table(parent_id)
);
Сначала создайте (временную) копию вашей старой таблицы со столбцом AUTO_INCREMENT id
:
create table tmp_table(
id int auto_increment primary key,
data json
);
При копировании из старой таблицы преобразуйте данные в JSON:
[ 113] Заполните parent_table
идентификаторами из tmp_table
:
insert into parent_table(parent_id)
select id from tmp_table;
Теперь (основная часть) заполните child_table
следующим запросом:
insert into child_table(parent_id, data)
with recursive seq(i) as ( -- sequence numbers 0 to 999
select 0
union all
select i + 1
from seq
where i < 999
)
select t.id as parent_id
, json_unquote(json_extract(t.data, concat('$[', s.i, ']'))) as data
from tmp_table t
join seq s on s.i <= json_length(t.data)-1;
child_table
теперь содержит следующие данные:
child_id parent_id data
1 1 ffffffff-11111-1111-baaa-xxxx
2 2 zxyvvv-1234567-abcdefghijk
3 2 1234567-abcdefg-hijklmn
Основная идея - объединить tmp_table
с последовательностью число от 0 до 999 (генерируется с помощью рекурсивного CTE) и используйте эти числа для извлечения соответствующих элементов из массива JSON.