SQL 插入值
SQL INSERT INTO VALUES
我有 2 个表 :
表用户
UserID
Name
FirstName
1
CAMERON
James
2
CONNOR
John
3
CONNOR
Sarah
表格目标
UserID
TargetName
TargetLocation
2
Someone...
Someplace...
2
Someone...
Someplace...
4
Someone...
Someplace...
3
Someone...
Someplace...
2
Someone...
Someplace...
我有一个 CSV 文件来完成我的 TargetTable。我认为通过格式化我的 CSV 来制作一个像
这样的 SQL 命令会很容易
INSERT INTO
TableTarget (UserID,TargetName,TargetLocation)
VALUES
(2,'Someone...','Someplace...'),
(2,'Someone...','Someplace...'),
(3,'Someone...','Someplace...')
但我的 CSV 文件中没有用户 ID,我有姓名和名字:
Name;FirstName;TargetName;TargetLocation
而且我不知道该怎么做。最糟糕的是,在我的一百个用户中,有时他们有相同的名字。
我很乐意提供一些帮助!
谢谢!
将 CSV table 加载到分段 table 中。然后使用 join
:
加载最终结果
insert into targettable (UserID, UserID TargetName TargetLocation, TargetLocation)
select u.userid, tts.TargetName, tts.TargetLocation
from targettable_staging tts join
tableuser u
on u.name = tts.name and u.firstname = tts.firstname;
您可能需要另一个步骤,将未知用户加载到用户 table 中。在整个过程中,您都会祈祷用户拥有唯一的名称。
我有 2 个表 :
表用户
UserID | Name | FirstName |
---|---|---|
1 | CAMERON | James |
2 | CONNOR | John |
3 | CONNOR | Sarah |
表格目标
UserID | TargetName | TargetLocation |
---|---|---|
2 | Someone... | Someplace... |
2 | Someone... | Someplace... |
4 | Someone... | Someplace... |
3 | Someone... | Someplace... |
2 | Someone... | Someplace... |
我有一个 CSV 文件来完成我的 TargetTable。我认为通过格式化我的 CSV 来制作一个像
这样的 SQL 命令会很容易INSERT INTO
TableTarget (UserID,TargetName,TargetLocation)
VALUES
(2,'Someone...','Someplace...'),
(2,'Someone...','Someplace...'),
(3,'Someone...','Someplace...')
但我的 CSV 文件中没有用户 ID,我有姓名和名字:
Name;FirstName;TargetName;TargetLocation
而且我不知道该怎么做。最糟糕的是,在我的一百个用户中,有时他们有相同的名字。 我很乐意提供一些帮助! 谢谢!
将 CSV table 加载到分段 table 中。然后使用 join
:
insert into targettable (UserID, UserID TargetName TargetLocation, TargetLocation)
select u.userid, tts.TargetName, tts.TargetLocation
from targettable_staging tts join
tableuser u
on u.name = tts.name and u.firstname = tts.firstname;
您可能需要另一个步骤,将未知用户加载到用户 table 中。在整个过程中,您都会祈祷用户拥有唯一的名称。