mysql - 带有嵌套选择的sql插入语句

标签 mysql sql

是否可以执行 1 条 sql 语句(插入),其中您复制了另一个表中的一个插入值和您硬编码的另一个值? 例如,profilepic,我想从另一个表数据中复制值。至于显示名称,我想硬编码。 这是我的 sql 语句:

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

来自 mysql 的错误信息:

Error code:1093. You can't specify target table 'registration' for update in from clause. 

最佳答案

在 values 子句中使用子查询将不起作用。您应该改用以下查询:

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

关于mysql - 带有嵌套选择的sql插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38477699/

相关文章:

Mysql - 将记录从 X 行更新到 Y 行?

sql - 我可以在 "IN"MySQL 语句中使用通配符吗?

mysql - 当满足特定条件时 MySQL 中的求和

php - 从我的 sql 表行中搜索至少一个逗号分隔值

sql - SQL 更新查询中的聚合函数?

sql - 如何获取新插入或已经存在的记录的标识值?

mysql - 内表连接出现意外结果

php - 如何在表单值中存储sql列名

java - hibernate :Cannot add or update a child row: a foreign key constraint fails

SQL 内存 - sys.dm_os_memory_objects