SQL:将数据从 1 个表复制到其他 3 个表

发布于 2024-10-22 06:03:24 字数 1038 浏览 0 评论 0原文

我一直在阅读 W3 Schools 的 SQL Insert INTO,根据我的理解,它会在新表中创建数据的备份副本。我有 300 多条记录要添加。

我有 4 张桌子,其中一张是我正在处理的来源。 1)卡访问,这是来源 2) pro_Profile,主要目标 3) pro_Email,次要目标 4)pro_Address,第三​​个目标

我无法控制这个数据库方案。重建所有内容以使用一张表然后使用三张表的工作量太大。另外,我添加的数据可能与现有数据重复,因此我们也需要跳过这些数据。

卡访问有以下字段: 映射到 pro_Profile 的 CSUID 映射到 pro_Profile 的名字 映射到 pro_Profile 的姓氏 映射到 pro_Profile 的 eName 映射到 pro_Email 的电子邮件地址 映射到 pro_Address 的联系电话号码

一旦我复制到 Pro_Profile 中,它就需要获取主键 profileID,它是对所有这些表中的配置文件的引用。

如何首先将所有这些数据复制到 pro_Profile 中,以便获得唯一的密钥 profileID(它是所有其他表的伪造密钥)?

到目前为止,这是我的代码:

SELECT
    FirstName,
    LastName,
    eName,
    CSUID
INTO

WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

影响 0 行的新查询

INSERT INTO pro_Profile ("firstName","lastName","userName","departmentID","csuID")
SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access"
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

I've been reading W3 Schools on SQL Insert INTO, which from my understanding creates a backup copy of data into a new table. I have over 300 records to add.

I have 4 tables, one of which is the source I am coping from.
1) Card Access, which is the source
2) pro_Profile, main dest
3) pro_Email, secondary dest
4) pro_Address, third dest

I have no control over this database schemea. It's too much work to rebuilt everything to work with 1 table then the three. Also the data I am adding maybe a duplicate of what exists, so we need to skip those too.

Card access has these fields:
CSUID which maps to pro_Profile
firstName which maps to pro_Profile
lastName which maps to pro_Profile
eName which maps to pro_Profile
Email Address which maps to pro_Email
Contact Phone Number which maps to pro_Address

Once I copy into Pro_Profile it'll need to get the primary key, profileID which is the reference to the profile accross all these tables.

How do I copy all this data first into the pro_Profile so I can get the unique key profileID which is a forgien key to all the other tables?

Here's my code so far:

SELECT
    FirstName,
    LastName,
    eName,
    CSUID
INTO

WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

New QUERY which affects 0 rows

INSERT INTO pro_Profile ("firstName","lastName","userName","departmentID","csuID")
SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access"
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2024-10-29 06:03:24

插入的格式将是

INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2

所以基本上您选择要插入的内容。您将能够将其作为所有表中的一条语句来执行,并且还可以添加一个 where 子句。另请查看问题的答案。

更新:
首先编写 sql 查询以获取要插入的行。因此,上面的查询:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access"
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

单独运行时应该返回您将插入的所有结果。当你运行它时你会得到什么?

更新:我认为你误解了不存在。查看这个。我认为你需要类似的东西:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access" ca
WHERE (
  Select count(*)
  From pro_Profile 
  WHERE firstName = ca.firstName
    AND LastName = ca.lastName
  ) = 0

更新:或者你应该能够这样做:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access" ca
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
 WHERE firstName = ca.firstName
   AND LastName = ca.lastName
)

The format for your insert will be

INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2

So basically you select what you want to insert. You will be able to do it as one statement from all the tables and can add a where clause also. Check out the answers to this question also.

UPDATE:
First write your sql query to get the rows you want to insert. So the query you have above:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access"
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
)

when ran alone should return all the results you will be inserting. What do you get when you just run that?

UPDATE: I think you are misunderstading not exist. Check out this. I think you need something like:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access" ca
WHERE (
  Select count(*)
  From pro_Profile 
  WHERE firstName = ca.firstName
    AND LastName = ca.lastName
  ) = 0

UPDATE: Or alternatively you should be able to do:

SELECT "First Name","Last Name","eName","Department","CSUID"
FROM "Card Access" ca
WHERE NOT EXISTS (
 Select
    firstName,
    LastName 
 From
    pro_Profile 
 WHERE firstName = ca.firstName
   AND LastName = ca.lastName
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文