Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql insert statement with nested select

Tags:

sql

mysql

is it possible to do 1 sql statement (insert) which you duplicate one of the inserting value from another table and another value you hard code? for eg, profilepic, i want to duplicate the value from another table data. As for displayname, i would like to hard code. This is my sql statement:

  insert into registration (profilePic, displayname) 
    values ( (select profilePic from registration where userId = 143), 'abc'  );

Error message from mysql :

Error code:1093. You can't specify target table 'registration' for update in from clause. 
like image 842
qcc Avatar asked May 18 '26 02:05

qcc


1 Answers

Using subqueries within the values clause will not work. You should use the following query instead:

insert into registration (profilePic, displayname) 
select (select profilePic from registration where userId = 143), 'abc'
like image 185
Cameron Tinker Avatar answered May 19 '26 15:05

Cameron Tinker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!