【问题标题】:How to permanently update the table structure after joining few columns from other table连接其他表中的几列后如何永久更新表结构
【发布时间】:2019-02-26 06:19:14
【问题描述】:

我有一个有 5 列的表,我正在加入另一个有 2 列的表。 (基于 1 个常用列)
现在,我在加入后从 table1 中获得了所有 5 列,并从 table2 中获得了 1 个额外列。

预期:
我需要我的 table1 结构是我加入的结果。 (即)我如何在加入后更新我的表结构。我要求所有 6 列都是表 1

【问题讨论】:

  • 您不能混合使用 DDL 和 DML。你是什​​么意思你想根据连接结果更新表结构?
  • 你能显示一些图像来清除吗? :) 会有很大帮助
  • 使用视图。无需更改表结构。
  • 最初,我的表有 5 列。加入的结果现在是 6 列。我需要我的第 6 列(我从 join 中获得)在我的 table1 中。

标签: mysql sql


【解决方案1】:

你应该使用视图:

create v_table1 as
    select t1.*, t2.col
    from table1 t1 join
         table2 t2
         on . . . ;

这很方便。当table2 更改时,视图将“自动”反映更改。

如果您想向table1 添加一个新列并更新一次,您可以这样做:

alter table table1 add <new column> <column definition>;

update table1 t1 join
       table2 t2
       on . . .
    set t1.<new column> = t2.col;

但是,这会在运行时设置一次值。

【讨论】:

  • 我需要它作为一个新的表结构的原因是,我还有许多其他的数据操作要对结果集进行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 2011-06-25
  • 2015-06-21
  • 2021-09-19
相关资源
最近更新 更多