'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 ?!
Solution 1:[1]
You have a syntax error at: select distinct services_f_service_id try this: 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>');
Solution 2:[2]
You can use a select command with no table here (make sure that the service_parameters has only one row for value='xxx'):
insert into service_parameters (
services_f_service_id,
parameter_type_f_type_id,
value)
select
(select distinct services_f_service_id from service_parameters where value = 'XXX'),
1,
'<url>';
;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jawwad Haider |
| Solution 2 |
