tags:

views:

129

answers:

2

Hey all-

I have a MySQL table that defines values for a specific customer type.

| CustomerType | Key Field 1 | Key Field 2 | Value |

Each customer type has 10 values associated with it (based on the other two key fields).

I am creating a new customer type (TypeB) that is exactly the same as another customer type (TypeA). I want to insert "TypeB" as the CustomerType but then just copy the values from TypeA's rows for the other three fields.

Is there an SQL insert statement to make this happen?

Somthing like:

insert into customers(customer_type, key1, key2, value)  values
"TypeB" union 
    select key1, key2, value
    from customers
    where customer_type = "TypeA"

Thanks- Jonathan

+3  A: 

You can select any constant value from a table:

insert into customers(customer_type, key1, key2, value)
    select "TypeB", key1, key2, value
    from customers
    where customer_type = "TypeA"
Peter Lang
A: 

From the MySQL reference:

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Basically exactly what you have except get rid of the 'TypeB" union' line.

Serguei