【问题标题】:MYSQL problem: How to add values from one table to anotherMYSQL问题:如何将一个表中的值添加到另一个表中
【发布时间】:2022-01-14 16:01:39
【问题描述】:

我可能有一个简单的问题: 我想将数据从一个表插入到另一个表中。

到这张表:

(ID_autor, name, surname)

从这里:

autor(ID_autor, name, surname, nationality, birthday)

这是我的代码:

INSERT INTO tab1 (ID_autor, name, forname)
SELECT ID_autor, name, forname, nationality, birthday
FROM autor;

但我不知道为什么不好?????

【问题讨论】:

  • 基本思路是正确的,但是您选择了 5 列并仅插入 3 列。您希望它如何工作?
  • 你插入的方向也与你想要的相反。
  • 我写错了,我改正了
  • 只需选择您要复制的 3 列。省略 nationalitybirthday
  • 但我想将表中的值插入到tab1中

标签: mysql select


【解决方案1】:

您应该只选择在目标表中具有相应列的列。由于tab1 中没有nationalitybirthday 列,因此将它们排除在select 列表之外。

INSERT INTO tab1 (ID_autor, name, forname)
SELECT ID_autor, name, forname
FROM autor;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2023-03-29
    相关资源
    最近更新 更多