我有一个包含 10 个字段的数据库,其中只有少数字段经常更改。我想打开当前记录,更新它,然后使用新的自动 ID 将其输入回数据库,同时保留以前的记录。
如果我插入新记录,我必须重新输入所有信息,即使它可能没有改变。 如果我更新一条记录,它会覆盖以前的值。
如有任何建议,我们将不胜感激
最佳答案
适当修改版本怎么样
insert into table select * from table where id='current_id'
示例查询:假设我想读取第一条记录,提升学生,主题,但更改标记并插入新记录
insert into score(student,subject,marks)
select student,subject,'30' from score where id=1;
Sample Data
+---------+---------+-------+----+
| student | subject | marks | id |
+---------+---------+-------+----+
| A1 | phy | 20 | 1 |
| A1 | bio | 87 | 2 |
| A2 | che | 24 | 3 |
| A3 | che | 50 | 4 |
Table structure:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| student | varchar(20) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| marks | int(1) | YES | | NULL | |
| id | int(10) | NO | PRI | NULL | auto_increment |
+---------+-------------+------+-----+---------+----------------+
关于mysql - 打开一条记录,更新它并在 MySQL 中作为新记录插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180125/