本文介绍了使用子查询 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
发布评论