在插入查询中使用选择查询以获取相同的表名 [英] Use select query inside the Insert query for the same table name

查看:46
本文介绍了在插入查询中使用选择查询以获取相同的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在插入"查询中使用选择"查询,但是条件是我想对选择"和插入"查询使用相同的表名.
例如
> mysql> insert into sample_elements (name,position,ownerel) values ('Namespace1', select id from sample_elements where name='Namespace1',0);

Is it possible to use a "select" query inside the "Insert" query, But the condition is i want to use the same table name for both "select" and "Insert" query.
For Example
mysql> insert into sample_elements (name,position,ownerel) values ('Namespace1', select id from sample_elements where name='Namespace1',0);

请对此进行指导.

推荐答案

将您的插入内容更改为如下所示:

Change your insert to look like the following :

insert into sample_elements (name,position,ownerel) select 'Namespace1',id,0 from sample_elements where name='Namespace1';

基本上不使用值,而仅使用select语句并将经过编码的值作为列插入

Basically instead of using values only use the select statement and insert the harcoded values as columns

这篇关于在插入查询中使用选择查询以获取相同的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆