2014-03-04 13 views
0

我有一個帶有10個字段的數據庫,其中只有少數更改頻繁。我想打開當前記錄,更新它,然後用新的自動編號將其輸入到數據庫中,同時保留以前的記錄。打開一條記錄,更新它並將其作爲新記錄插入到MySQL中

如果我插入新記錄,我必須重新輸入所有信息,即使它可能沒有改變。 如果我更新記錄,它將覆蓋以前的值。

任何建議,將不勝感激

+0

http://dev.mysql.com/doc/refman/5.0/en/cursors.html – Tony

+0

正確的是,當您插入記錄時,您在這種情況下正在執行的操作中,必須爲所有列設置值你不需要默認值。你有什麼問題? –

回答

0

如何

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 | 
+---------+-------------+------+-----+---------+----------------+ 
+0

'INSERT INTO table SELECT * FROM table'。注意,'*'意味着你沒有主鍵列會創建一個重複的鍵。這很少見。很可能,您需要命名這些列。 –

+0

@MarcusAdams ..編輯使事情更清晰...希望這個例子使事情變得更加清晰.. –

相關問題