Er is geen PHP nodig. Je kunt het alleen doen met pure MySQL-code.
Tafel maken/tabel invoegen
CREATE TABLE HugeTable
(`Column1` VARCHAR(11), `Column2` VARCHAR(11), `Column3` VARCHAR(11))
;
INSERT INTO HugeTable
(`Column1`, `Column2`, `Column3`)
VALUES
('Data1;Data2', 'Data3;Data4', 'Data5;Data6')
;
CREATE TABLE NewTable
(`Column1` VARCHAR(11), `Column2` VARCHAR(11), `Column3` VARCHAR(11))
;
Eerst hebben we MySQL nodig om getallen te genereren. Deze MySQL-code genereert 1 tot 100. De uiteindelijke query ondersteunt dus maximaal 100 gescheiden waarden.
Zoekopdracht
SELECT
@row := @row + 1 AS ROW
FROM (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row2
CROSS JOIN (
SELECT @row := 0
) init_user_params
Resultaat
row
--------
1
2
3
4
5
6
7
8
9
10
...
...
90
91
92
93
94
95
96
97
98
99
100
Nu kunnen we kijken naar een methode om te scheiden op de; scheidingsteken. We kunnen daarvoor geneste SUBSTRING_INDEX-functies gebruiken
Zoekopdracht
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('Data1;Data2', ';', 1), ';', -1) AS DATA
Resultaat
data
--------
Data1
U kunt zien dat alleen het eerste woord wordt geretourneerd als we het tweede woord willen dat we kunnen gebruiken
Zoekopdracht
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('Data1;Data2', ';', 2), ';', -1) AS DATA
Resultaat
data
--------
Data2
Nu combineren we de nummergenerator en de SUBSTRING_INDEX om de gegevens te genereren
Zoekopdracht
SELECT
DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(Column1, ';', rows.row), ';', -1) Column1
, SUBSTRING_INDEX(SUBSTRING_INDEX(Column2, ';', rows.row), ';', -1) Column2
, SUBSTRING_INDEX(SUBSTRING_INDEX(Column3, ';', rows.row), ';', -1) Column3
FROM (
SELECT
@row := @row + 1 AS ROW
FROM (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row2
CROSS JOIN (
SELECT @row := 0
) init_user_params
)
ROWS
CROSS JOIN
HugeTable
Resultaat
Column1 Column2 Column3
------- ------- ---------
Data1 Data3 Data5
Data2 Data4 Data6
Nieuwe tabel opvragen
INSERT INTO
NewTable
SELECT
DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(Column1, ';', rows.row), ';', -1) Column1
, SUBSTRING_INDEX(SUBSTRING_INDEX(Column2, ';', rows.row), ';', -1) Column2
, SUBSTRING_INDEX(SUBSTRING_INDEX(Column3, ';', rows.row), ';', -1) Column3
FROM (
SELECT
@row := @row + 1 AS ROW
FROM (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row2
CROSS JOIN (
SELECT @row := 0
) init_user_params
)
ROWS
CROSS JOIN
HugeTable
Zoekopdracht
SELECT * FROM NewTable
Resultaat
Column1 Column2 Column3
------- ------- ---------
Data1 Data3 Data5
Data2 Data4 Data6