Tidak diperlukan PHP. Anda dapat melakukannya hanya dengan kode MySQL murni.
Buat tabel/sisipkan tabel
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))
;
Pertama kita membutuhkan MySQL untuk menghasilkan angka. Kode MySQL ini menghasilkan 1 hingga 100. Jadi kueri akhir akan mendukung hingga 100 nilai yang dipisahkan.
Kueri
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
Hasil
row
--------
1
2
3
4
5
6
7
8
9
10
...
...
90
91
92
93
94
95
96
97
98
99
100
Sekarang kita dapat melihat metode untuk memisahkan pada; pembatas. Kita dapat menggunakan fungsi SUBSTRING_INDEX bersarang untuk itu
Kueri
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('Data1;Data2', ';', 1), ';', -1) AS DATA
Hasil
data
--------
Data1
Anda dapat melihat hanya kata pertama yang dikembalikan jika kita ingin kata kedua kita dapat menggunakan
Kueri
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('Data1;Data2', ';', 2), ';', -1) AS DATA
Hasil
data
--------
Data2
Sekarang kita gabungkan generator angka dan SUBSTRING_INDEX untuk menghasilkan data
Kueri
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
Hasil
Column1 Column2 Column3
------- ------- ---------
Data1 Data3 Data5
Data2 Data4 Data6
Tabel Baru Kueri
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
Kueri
SELECT * FROM NewTable
Hasil
Column1 Column2 Column3
------- ------- ---------
Data1 Data3 Data5
Data2 Data4 Data6