简体   繁体   English

sql中insert语句内的select语句返回多行

[英]select statement inside insert statement in sql returns multiple rows

I have a following structured query in mysql. 我在mysql中有以下结构化查询。 But its returning a error. 但是它返回一个错误。 Basically, I want to get value from existing table and instert into new table. 基本上,我想从现有表中获取价值并插入新表中。 I tried the following, but got error; 我尝试了以下操作,但出现错误;

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

I tested but the error is : #1242 - Subquery returns more than 1 row . 我测试过,但错误是: #1242 - Subquery returns more than 1 row The problem is I am getting more than single record from the select query inside insert . 问题是我从insert内的select查询中获得的记录不止一个。 How can I remove such error. 我该如何消除这种错误。

the total query looks something like below. 总查询如下所示。 where you have so replace the * with column names you want to select! 在这里,请用您要选择的列名称替换*!

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
)

I moved you're select statements into a subquery so you can use the total result of the subquerys to you're advantage. 我将您的select语句移到了子查询中,因此您可以利用子查询的总结果来发挥自己的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM