2016-03-16 112 views
1

我在mysql中有一個如下的結構化查詢。但它返回一個錯誤。基本上,我想從現有的表中獲得價值,並插入新表中。我嘗試了以下,但得到錯誤;sql語句中的insert語句返回多行

INSERT INTO `table1`(
    `first`,`second`,`third`) VALUES(

    (SELECT table2.timemodified FROM `xtable` AS table2,`ytable` AS table3 
     WHERE table3.id = table2.contextid), 

    (SELECT table4.id FROM `ztable` AS table4,`ytable` AS table3 WHERE table4.id = table3.instanceid), 

    (SELECT murs.id FROM `table5` AS murs, 
    `xtable` AS table2, 
    `wtable` AS table6, 
    `ytable` AS table3, 
    `vtable` AS table7 
    WHERE murs.id = table2.userid AND table6.id = table2.roleid AND table3.id = table2.contextid AND table7.instance = table3.instanceid AND table6.id =3) 
); 

我測試過,但錯誤是:#1242 - Subquery returns more than 1 row。問題是我從insert內部的select查詢得到的單個記錄超過了單個記錄。我怎樣才能刪除這樣的錯誤。

+0

你想插入多行ro只是一行。 – lordkain

+0

是的,我想插入多行 – vijayrana

+0

然後使用:插入表(值1,值2,值3)從表2中選擇x,y,z – lordkain

回答

2

總查詢看起來如下所示。在那裏你可以用你想要選擇的列名替換*。

INSERT INTO table1(first,second,third) 

-- replace * with columns name first,second,third 
select * from (
-- START YOU'RE select query 
(SELECT table2.timemodified FROM `xtable` AS table2,`ytable` AS table3 
     WHERE table3.id = table2.contextid), 

    (SELECT table4.id FROM `ztable` AS table4,`ytable` AS table3 WHERE table4.id = table3.instanceid), 

    (SELECT murs.id FROM `table5` AS murs, 
    `xtable` AS table2, 
    `wtable` AS table6, 
    `ytable` AS table3, 
    `vtable` AS table7 
    WHERE murs.id = table2.userid AND table6.id = table2.roleid AND table3.id = table2.contextid AND table7.instance = table3.instanceid AND table6.id =3) 
-- END YOU'RE select query 
) 

我搬到你的SELECT語句爲子查詢,所以你可以給你的優勢使用subquerys的總結果。

+1

請你解釋:) – vijayrana