Open a record, update it and insert as new record in MySQL

I have a db with 10 fields, of which only a few change very often. I would like to open the current record, update it, and enter it back into the db with a new auto id while retaining the previous record.

If I INSERT a new record, I have to re-enter all the info even tho it may not have changed. If I UPDATE a record, it overwrites the previous values.

Any suggestion would be greatly appreciated

Answers


How about a suitably modified version of

insert into table select * from table where id='current_id'

Sample query:Say i want to read first record ,lift student, subject but change marks and insert a new record

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

You can do it via two different queries: SELECT and INSERT, or you can use INSERT INTO SELECT construction. Check documentation for complete reference http://dev.mysql.com/doc/refman/5.0/en/insert-select.html


Need Your Help

Detecting client disconnects within gevent

python exception-handling gevent disconnect

How should I detect if a client has disconnected from my gevent webserver if I use a queue to generate response data? Right now I keep getting the Broken pipe error, but I don't quite understand wh...

Display xml feed with xslt

xml xslt

I need to display a xml feed with xslt, the xml feed requires authentication is this possible?