首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个表之间无类型all of结构复制值解决方案

问两个表之间无类型all of结构复制值解决方案
EN

Stack Overflow用户
提问于 2012-09-10 12:20:18
回答 1查看 91关注 0票数 0

我使用下面的代码

代码语言:javascript
复制
update 
contracts a, 
contracts_history b 
set 
a.name_surname=b.name_surname 

我的表有64列,我正在寻找一种解决方案来复制所有数据,而不必指定列名-沿着以下行:

代码语言:javascript
复制
 $sql = "INSERT INTO `contracts_history` 
         SELECT * FROM `contracts` WHERE id='$contract_id'";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-10 12:33:42

只要表的字段数与数据类型匹配的字段数完全相同,就可以使用insert into select...语法-否则,必须使用列名来指定要复制的内容。

我只是运行以下示例来向您展示语法:

代码语言:javascript
复制
mysql> use test
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table test1 (id int(2), varry varchar(3));
Query OK, 0 rows affected (0.08 sec)

mysql> create table test2 (id int(2), barry varchar(3));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1,'aaa');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1;
Empty set (0.00 sec)

mysql> insert into test1 (select * from test2);
Query OK, 1 row affected (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+------+-------+
| id   | varry |
+------+-------+
|    1 | aaa   |
+------+-------+
1 row in set (0.00 sec)

mysql> alter table test2 add column third int(1);
Query OK, 1 row affected (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> update test2 set barry='ccc';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> insert into test1 (select * from test2);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12345299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档