【问题标题】:How to copy data from table to another如何将数据从表复制到另一个
【发布时间】:2017-03-07 04:20:50
【问题描述】:

如何将数据从表复制到另一个

  1. 将所有讲师(与学生具有相同 ID 的除外)插入同一部门中 tot_creds = 0 的学生表中

  2. 现在删除上面所有新添加的“学生”(注意:不应删除已存在的 tot_creds = 0 的学生)

导师表:

 ID      Name           dept_name    salary
10101   Srinivasan     Comp. Sci.   65000.00
12121   Wu             Finance      90000.00
15151   Mozart         Music        40000.00
22222   Einstein       Physics      95000.00
32343   El Said        History      60000.00
33456   Gold           Physics      87000.00
45565   Katz           Comp. Sci.   75000.00
58583   Califieri      History      62000.00
76543   Singh          Finance      80000.00

学生:

ID      Name      dept_name    tot_cred
00128   Zhang     Comp. Sci.    102
12345   Shankar   Comp. Sci.    32
19991   Brandt    History       80 
23121   Chavez    Finance       110
44553   Peltier   Physics       56
45678   Levy      Physics       46
70557   Snow      Physics       0

我已经尝试过这个插入,但没有任何反应 0 行受影响请指导我

insert into student select ID, name, dept_name, 0 
 from instructor 
  where ID != instructor.ID 

【问题讨论】:

  • 这个查询永远不会有任何行。 ID 的值不可能不等于 ID。对于这种类型的事情,您需要使用左连接。
  • 这是 MySQL 还是 Microsoft SQL Server?
  • 我想要在 mysql 和 sql 中
  • 我不知道您会找到对两者都适用的相同代码。 mysql和sql server有很大的不同。在 sql server 中,我将使用 OUTPUT 语句来捕获新插入行的 ID。然后你可以很容易地加入它来删除它们。或者只是用回滚包装插入事务。 :)
  • 什么是 mysql

标签: sql-server database


【解决方案1】:

我会使用not exists 子句过滤掉现有记录。

insert into Student (ID, name, dept_name, tot_cred)
select ID, name, dept_name, 0
from Instructor
where not exists (
select 1
from Student
where ID = Instructor.ID
)

然后将它们从 Instructors 表中删除(如果我正确理解第 2 部分)。

delete
from Instructor
where exists (
select 1
from Student
where ID = Instructor.ID
)

不过,这假设 ID 在 Student 表和 Instructor 表之间是全局唯一的。

【讨论】:

  • 此代码有效,但我想从我们最近插入的学生表中删除数据。
  • 那么,如果您只是要立即删除它们,为什么还要插入它们呢?但是,如果是这样,您可能需要查看output clause
  • 实际上我这样做是为了我的数据库实践。
【解决方案2】:

我想你想要这个:

INSERT INTO student (id, name, dept_name, tot_cred)
   SELECT instructor.ID, instructor.name, instructor.dept_name, 0 
   FROM instructor LEFT JOIN student on instructor.ID = student.ID
   WHERE student.ID IS NULL
     AND instructor.tot_cred <> 0

这将插入总学分不为零且与现有学生不匹配的教师。你的 #2 有点神秘,所以我猜你打算用那部分做什么。

【讨论】:

    【解决方案3】:
    INSERT INTO Students
        (ID, Name, dept_name, salary)
    SELECT 
        I.ID, I.Name, I.dept_name, I.salary 
    FROM 
        Instructors I
    LEFT JOIN  
        Students S ON S.ID = I.ID 
    WHERE 
        S.ID IS NULL
        AND S.tot_cred <> 0
    
    
    DELETE FROM  Instructors WHERE ID IN (SELECT ID FROM STUDENT) 
    

    【讨论】:

      【解决方案4】:

      您可以使用以下方法将数据从一个表复制到另一个表:

      --Insert data from one table to another table (same or different dBs)
      INSERT INTO DestinationDB.dbo.DestinationTable(ColumnX, ColumnY, ColumnZ, ColumnW, ColumnT)
      --If the ID column of the DestinationTable is automatically created (isIdentity) do not insert value to the ID column.
      SELECT ColumnA, ColumnB, ColumnC, 'Test', 1 
      FROM SourceDB.dbo.SourceTable
      WHERE < Search Conditions >
      

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多