2017-10-21 4 views
0

Ich habe eine Auswahlabfrage, die mich 8 Spalten bringt:Legen Sie mehrere Spalten in einer MySQL

SELECT 
substring_index(`Cuisines`,',',1), 
substring_index(substring_index(`Cuisines`,',',-7),',',1), 
substring_index(substring_index(`Cuisines`,',',-6),',',1), 
substring_index(substring_index(`Cuisines`,',',-5),',',1), 
substring_index(substring_index(`Cuisines`,',',-4),',',1), 
substring_index(substring_index(`Cuisines`,',',-3),',',1), 
substring_index(substring_index(`Cuisines`,',',-2),',',1), 
substring_index(substring_index(`Cuisines`,',',-1),',',1) 
FROM `dump`) 

und ich möchte sie alle in einer einzigen Spalte in einer anderen Tabelle einfügen, so versuche ich, aber zu tun, verschmelzen nicht sie zusammen, sondern jeder in eine neue Zeile gehen würde, also, was ich tun wollte war:

INSERT INTO cuisines(name) VALUES (
(SELECT 
substring_index(`Cuisines`,',',1), 
substring_index(substring_index(`Cuisines`,',',-7),',',1), 
substring_index(substring_index(`Cuisines`,',',-6),',',1), 
substring_index(substring_index(`Cuisines`,',',-5),',',1), 
substring_index(substring_index(`Cuisines`,',',-4),',',1), 
substring_index(substring_index(`Cuisines`,',',-3),',',1), 
substring_index(substring_index(`Cuisines`,',',-2),',',1), 
substring_index(substring_index(`Cuisines`,',',-1),',',1) 
FROM `dump`)) 

gibt es eine Möglichkeit, dass allein auf MySQL zu tun?

+1

Haben Sie versucht, 'UNION' und eine' SELECT' Abfrage für einen Ihrer Teilstrings zu verwenden? – Moseleyi

+0

Das klingt nach einer schrecklichen Idee – Strawberry

Antwort

0
INSERT INTO cuisines(name) 
VALUES ((SELECT substring_index(`Cuisines`,',',1) FROM `dump`)); 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-7),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-6),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-5),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-4),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-3),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-2),',',1)FROM `dump`)) 
INSERT INTO cuisines(name) VALUES (
(SELECT substring_index(substring_index(`Cuisines`,',',-1),',',1) FROM `dump`)) 
+0

'# 1242 - Unterabfrage gibt mehr als 1 Zeile zurück ' –

+0

Ich habe Ihre Antwort bearbeitet, Sie brauchten' VALUES' nicht drin. –

Verwandte Themen