INSERT INTO with SubQuery MySQL(使用子查询 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用子查询 MySQL 插入 INTO


基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01