Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top