使用子查询 MySQL 插入 INTO

编程入门 行业动态 更新时间:2024-10-26 20:35:25
本文介绍了使用子查询 MySQL 插入 INTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个声明:

INSERT INTO qa_costpriceslog (item_code, invoice_code, item_costprice) VALUES (1, 2, (SELECT item_costprice FROM qa_items WHERE item_code = 1));

我正在尝试插入与 item_costprice 相同数据的值副本,但显示错误:

I'm trying to insert a value copy the same data of item_costprice, but show me the error:

Error Code: 1136. Column count doesn't match value count at row 1

我该如何解决这个问题?

How i can solve this?

推荐答案

在 SELECT 语句中使用带有别名的数字文字.SELECT 组件周围不需要 ().

Use numeric literals with aliases inside a SELECT statement. No () are necessary around the SELECT component.

INSERT INTO qa_costpriceslog (item_code, invoice_code, item_costprice) SELECT /* Literal number values with column aliases */ 1 AS item_code, 2 AS invoice_code, item_costprice FROM qa_items WHERE item_code = 1;

请注意,在 INSERT INTO...SELECT 的上下文中,别名实际上不是必需的,您只需 SELECT 1, 2, item_costprice,但在正常的 SELECT 你需要别名来访问返回的列.

Note that in context of an INSERT INTO...SELECT, the aliases are not actually necessary and you can just SELECT 1, 2, item_costprice, but in a normal SELECT you'll need the aliases to access the columns returned.

更多推荐

使用子查询 MySQL 插入 INTO

本文发布于:2023-06-01 10:30:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!