PDA

View Full Version : SQL*plus Inserting new row with values = to existing rows data


Still
12-05-2005, 09:03 PM
Hi all

I am trying to insert a new employee into an empolyee table and get some of the values (namely phone no.) to be the same as what another employee currently has. This new employee is replacing the old employee, but i have to keep the old employee alive in the db as other data is reliant on its existance.

I was wondering how do i insert the new employee asking the, VALUE ('etc', 'etc',) line, to assign a value to certain fields equal to the value contained in other employee rows.

Any help would be great.

Thanks

vinyl-junkie
12-06-2005, 03:57 AM
Do a select on the old employee record.

Assign variables to the database elements you want to assign to the new employee. For example:

$phone = $row['phone'];

Insert the new employee record using values of $phone, etc.

Q. - Do you need to clear those fields on the old employee record? If so, update the old record and assign those fields a value of NULL.

Velox Letum
12-06-2005, 05:51 AM
Or you could use an INSERT SELECT...
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

INSERT INTO employees (name,phone) SELECT 'NewName',employees.phone FROM employees WHERE employee_id = '0'

Think something along those lines'd work.

vinyl-junkie
12-06-2005, 05:55 AM
Or you could use an INSERT SELECT...
That code works only for MySQL 5.0. Make sure what version of MySQL you're running before you try it.

Velox Letum
12-06-2005, 07:47 AM
I found the same page on the 3.23/4.0/4.1 section as well, so I'd assume it'd work for them as well.