2017-10-21 101 views
0

我有給我帶來了8列的選擇查詢:插入多列到一個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`) 

,我想他們都插入到另一個表中的單個列,所以我想這樣做,但不會合並在一起,而是每一個將進入一個新的行,所以我試圖做的是:

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`)) 

有沒有辦法獨自做在MySQL?

+1

您是否嘗試過使用'UNION'和一個'SELECT'查詢每個子串? – Moseleyi

+0

這聽起來像一個可怕的想法 – Strawberry

回答

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 - 子查詢返回多於一行' –

+0

我編輯了你的答案,你不需要在那裏有'VALUES'。 –