Mysql 嵌套查询问题
下面是我的查询,我收到“子查询返回超过 1 行”错误:
INSERT INTO billing_temp (`billing_period_id`,`soc_id`,`bill_y_n`)
VALUES(
(SELECT `id` FROM `billing_period` ORDER BY `billing_start_date` DESC LIMIT 0,1),
(SELECT `id` FROM `milk_producer` WHERE active='1'),
'N'
)
SELECT `id` FROM `billing_period` ORDER BY `billing_start_date` DESC LIMIT 0,1
返回多个值。
我希望这些多个值将插入到表中,例如:
1 03 N
2 03 N
3 03 N
below is my query and I am getting a "Subquery returns more than 1 row" error:
INSERT INTO billing_temp (`billing_period_id`,`soc_id`,`bill_y_n`)
VALUES(
(SELECT `id` FROM `billing_period` ORDER BY `billing_start_date` DESC LIMIT 0,1),
(SELECT `id` FROM `milk_producer` WHERE active='1'),
'N'
)
SELECT `id` FROM `billing_period` ORDER BY `billing_start_date` DESC LIMIT 0,1
returns multiple values.
I want that these multiple values will insert into table, e.g:
1 03 N
2 03 N
3 03 N
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您无法执行
INSERT INTO ... SELECT
和以及INSERT INTO ... VALUES
。选择一个。在您的情况下,按行插入数据时存在不合逻辑的相关性 - 除非
milk_ Producer
只有 1 行WHERE active='1'
。同样,如果您尝试加载多行,为什么要将内部查询LIMIT
限制为 1 个结果?The problem is you can't perform an
INSERT INTO ... SELECT
and andINSERT INTO ... VALUES
. Choose one.In your case, there is an illogical correlation upon inserting data by row -- unless
milk_producer
only has 1 rowWHERE active='1'
. Similarly, if you're trying to load multiple rows, why are youLIMIT
ing the inner query to 1 result?您需要循环执行
SELECT
语句。看看光标。http://dev.mysql.com/doc/refman/5.0/en /cursors.html
You need to loop through the
SELECT
statement. Have a look at cursors.http://dev.mysql.com/doc/refman/5.0/en/cursors.html