![](/img/trans.png)
[英]How Can I Combine a SELECT Statement that Returns Multiple Rows with an INSERT Statement?
[英]select statement inside insert statement in sql returns multiple rows
我在mysql中有以下結構化查詢。 但是它返回一個錯誤。 基本上,我想從現有表中獲取價值並插入新表中。 我嘗試了以下操作,但出現錯誤;
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
查詢中獲得的記錄不止一個。 我該如何消除這種錯誤。
總查詢如下所示。 在這里,請用您要選擇的列名稱替換*!
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語句移到了子查詢中,因此您可以利用子查詢的總結果來發揮自己的優勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.