Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从SELECT中插入SQL_Mysql_C# 4.0 - Fatal编程技术网

Mysql 从SELECT中插入SQL

Mysql 从SELECT中插入SQL,mysql,c#-4.0,Mysql,C# 4.0,假设在我的代码中有用户名和值。现在我需要将其保存在db中,但我需要首先获取对应于该用户名的ID,因为该表使用pk/fk链接到users表。我该怎么做? 我知道你可以做一个INSERT(blah)SELECT等来做,但这看起来像是一个直接的副本,我需要插入fk列的值作为SELECT的结果 用户表: [用户ID(pk),用户名] 化身表: [用户ID(fk),虚拟现实] 我需要 插入到虚拟表格(用户ID、虚拟表格) 值(*用户名为'theirname'*,'http://www.blah.com)

假设在我的代码中有用户名和值。现在我需要将其保存在db中,但我需要首先获取对应于该用户名的ID,因为该表使用pk/fk链接到users表。我该怎么做? 我知道你可以做一个INSERT(blah)SELECT等来做,但这看起来像是一个直接的副本,我需要插入fk列的值作为SELECT的结果

用户表: [用户ID(pk),用户名]

化身表: [用户ID(fk),虚拟现实]

我需要
插入到虚拟表格(用户ID、虚拟表格)
值(*用户名为'theirname'*,'http://www.blah.com)


谢谢你可能正在找这个

insert into myDestTable (userid, name, value, othercolumns)
select us.userid, us.name,'myvaluefromcode', othercolumns
from users us 
where us.name = 'mynamefromcode'

你可能在找这个

insert into myDestTable (userid, name, value, othercolumns)
select us.userid, us.name,'myvaluefromcode', othercolumns
from users us 
where us.name = 'mynamefromcode'

不必是直接抄袭。只需使select语句的结果与目标表的列匹配即可

e、 g


不必是直接抄袭。只需使select语句的结果与目标表的列匹配即可

e、 g


假设您的表的结构如下:

users:
    user_id           integer
    user_name         varchar(50)
    user_other_data   varchar(999)
    primary key       (user_id)
    unique            (user_name)
other_table:
    user_value        varchar(999)
    user_id           integer foreign key (users.user_id)
您需要的是能够在仅给定值和用户名的情况下,将条目插入到
other_表中

重要的是,您要以原子的方式执行此操作,以确保用户id/用户名映射不会在您下面更改。换句话说,你不能做:

get user_id from users based on user_name.
insert value and user_id into other_table.
由于用户id或用户名可能会在上述两个步骤之间更改(物理行的用户id可能不会更改,但表可能会从以下位置更改:

user_id  user_name
-------  ---------
1        bob
2        george
致:

因此,要实现原子化,它必须位于单个SQL语句中(或事务的一部分,但在这种情况下可以避免事务):

对于您的具体案例(在我最初的回答之后添加),您将看到:

insert into AvatarTable (UserID,AvatarURL)
    select UserID, :localAvatarURL
    from UserTable
    where UserName = :localUserName;

假设您的表的结构如下:

users:
    user_id           integer
    user_name         varchar(50)
    user_other_data   varchar(999)
    primary key       (user_id)
    unique            (user_name)
other_table:
    user_value        varchar(999)
    user_id           integer foreign key (users.user_id)
您需要的是能够在仅给定值和用户名的情况下,将条目插入到
other_表中

重要的是,您必须以原子方式执行此操作,以确保用户id/用户名映射不会在您下面更改。换句话说,您不能执行以下操作:

get user_id from users based on user_name.
insert value and user_id into other_table.
由于用户id或用户名可能会在上述两个步骤之间更改(物理行的用户id可能不会更改,但表可能会从以下位置更改:

user_id  user_name
-------  ---------
1        bob
2        george
致:

因此,要实现原子化,它必须位于单个SQL语句中(或事务的一部分,但在这种情况下可以避免事务):

对于您的具体案例(在我最初的回答之后添加),您将看到:

insert into AvatarTable (UserID,AvatarURL)
    select UserID, :localAvatarURL
    from UserTable
    where UserName = :localUserName;

我们可以有一些样本数据和数据结构信息吗?我们可以有一些样本数据和数据结构信息吗?是的,我们在等待10分钟左右通过后才允许我。是的,我们在等待10分钟左右通过后才允许我。