jared_flack
jared_flack

Reputation: 1646

MYSQL, Duplicating records but changing a column's value

I'm trying to write an SQL statement that duplicates all rows WHERE employee = 16(i.e.), but the new rows would have a different employee value.

Table before INSERT:

| employee | property_name | property_value |
|:--------:|:--------------|:---------------|
| 16       | Salary        | 28,000         |
| 16       | Department    | 12             |
| 17       | Salary        | 38,000         |
| 17       | Department    | 8              |

Desired outcome after INSERT:

| employee | property_name | property_value |
|:--------:|:--------------|:---------------|
| 16       | Salary        | 28,000         |
| 16       | Department    | 12             |
| 17       | Salary        | 38,000         |
| 17       | Department    | 8              |
| 18       | Salary        | 28,000         |
| 18       | Department    | 12             |

I've seen some threads that use variables. Could I set and reference a variable somehow that would replace values from an insert/select?

The answer to this thread looks like it would work. But I'd rather not create and drop tables like that.

Upvotes: 1

Views: 61

Answers (1)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

insert into YourTable (employee,property_name, property_value)
select 18, property_name, property_value from YourTable where employee = 16

Upvotes: 4

Related Questions