一种从一个表中选择字段并插入到另一个表的方法?

发布于 2024-12-24 16:48:20 字数 438 浏览 1 评论 0原文

我有两个表,TableA 和 TableB

TableA 有 9 个字段 TableB 有 7 个字段

两个表中有 2 个相同的字段(id 和 name),有没有办法从 TableA 中仅选择这两个字段并将它们插入到 TableB 中?

我已经使用此语句查看了 INSERT INTO...SELECT 方法:

INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1

但出现以下错误:

#1136 - Column count doesn't match value count at row 1

我认为此错误不允许我仅在表中插入 2 个字段?如果是这样,有没有办法解决这个问题或替代方法?

谢谢

I have two tables, TableA and TableB

TableA has 9 fields
TableB has 7 fields

There are 2 fields (id and name) that are identical in both tables, is there a way to select ONLY these two fields from TableA and insert them into TableB?

I have looked at the INSERT INTO... SELECT method using this statement:

INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1

But I get the following error:

#1136 - Column count doesn't match value count at row 1

I assume this error is not allowing me to insert only 2 fields into the table? If so, is there a way around this or an alternative method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2024-12-31 16:48:20

尝试:

INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;

必须假设 TableB 中的列名称与 TableA 匹配,否则您需要输入正确的名称。

Try:

INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;

One would have to assume that the column names in TableB match TableA otherwise you would need to put in the right names.

一场春暖 2024-12-31 16:48:20

您需要指定 TableB 的列名称(并且可能在 WHERE 子句中指定 TableA.id):

INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1

You need to specify the column names for TableB (and possibly specify TableA.id in the WHERE clause):

INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1
内心旳酸楚 2024-12-31 16:48:20

指定表b中的列

INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1

Specify the columns in table b

INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文