Skip to content
Advertisement

INSERT and SELECT on the same table in one SQL statement

I have the following SQL query :

insert into service_parameters (
    services_f_service_id,
    parameter_type_f_type_id,
    value)
values ( 
    (select distinct services_f_service_id from service_parameters where value = 'XXX'), 
    1, 
    '<url>');

MySQL is complaining with the following error:

SQL Error [1093] [HY000]: You can't specify target table 'service_parameters' for update in FROM clause

Is there a way to achieve this without spliting the SQL statment into 2 ?!

Advertisement

Answer

INSERT INTO service_parameters (
    services_f_service_id,
    parameter_type_f_type_id,
    value)
SELECT DISTINCT services_f_service_id,
                1, 
                '<url>'
FROM service_parameters 
WHERE value = 'XXX';
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement