MYSQL INSERT INTO 现有表数据 FROM 2 个单独的表 ON id

发布于 2024-11-08 23:03:15 字数 1081 浏览 0 评论 0原文

我有一个空表“new_foobar”。它有“fbc_primary_key”、“fbc_two”、“fbc_third”和“fbc_four”列。

fbc_third 默认为“apple”,

然后我有一个表“foo”,其中包含“fc_pimary_key”、“fc_two”列。还有一个表“bar”,其中包含“bc_primary_key”、“bc_two”、“bc_ Three”列。

fbc_primary_key、bc_primary_key 和 fc_primary_key 都是各自表的主键。

如何将 fc_two 插入到 fbc_two 中,将 bc_ Three 插入到 fbc_four 中。

TABLE foo
fc_primary_key | fc_two |
-------------------------
1              | hello  |
2              | goodbye|

TABLE bar
bc_primary_key | bc_two | bc_three |
------------------------------------
1              | abc    | 123      |
2              | def    | 456      |

*NOTE bar and foo have a 1-to-1 relationship.


TABLE foobar
fbc_primary_key | fbc_two | fbc_three | fbc_four |
---------------------------------------
/* empty */

...AFTER SOME SQL MAGIC....

TABLE foobar
fbc_primary_key | fbc_two | fbc_three | fbc_four |
---------------------------------------------------
1               | hello   | apple     | 123      |
2               | goodbye | apple     | 456      |

我遇到的问题是,如何让数据列落在 foobar 列下。

I have a table 'new_foobar' that is empty. it has columns 'fbc_primary_key', 'fbc_two', fbc_three, and fbc_four.

fbc_three defaults to 'apple'

then I have a table 'foo', with a columns 'fc_pimary_key', 'fc_two'. And a table 'bar' with columns 'bc_primary_key', 'bc_two', 'bc_three'.

fbc_primary_key, bc_primary_key, and fc_primary_key, are all primary keys of their respective tables.

how does one insert fc_two, into fbc_two, and bc_three into fbc_four.

TABLE foo
fc_primary_key | fc_two |
-------------------------
1              | hello  |
2              | goodbye|

TABLE bar
bc_primary_key | bc_two | bc_three |
------------------------------------
1              | abc    | 123      |
2              | def    | 456      |

*NOTE bar and foo have a 1-to-1 relationship.


TABLE foobar
fbc_primary_key | fbc_two | fbc_three | fbc_four |
---------------------------------------
/* empty */

...AFTER SOME SQL MAGIC....

TABLE foobar
fbc_primary_key | fbc_two | fbc_three | fbc_four |
---------------------------------------------------
1               | hello   | apple     | 123      |
2               | goodbye | apple     | 456      |

The problem I am having with is, how to get the columns of data to fall under the foobar column.

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

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

发布评论

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

评论(2

鹤仙姿 2024-11-15 23:03:15
insert into foobar
select 
  ifnull(f.fbc_primary_key, b.fbc_primary_key), 
  fbc_two, 
  fbc.three
from 
  foo f
  full outer join bar b on b.fbc_primary_key  = f.fbc_primary_key 

如果您只需要两个表中都存在的记录,则可以使用内连接。您也可以跳过 ifnull,因为 f 和 b 都会有一个 id。

insert into foobar
select 
  ifnull(f.fbc_primary_key, b.fbc_primary_key), 
  fbc_two, 
  fbc.three
from 
  foo f
  full outer join bar b on b.fbc_primary_key  = f.fbc_primary_key 

If you only want records that exist in both tables, you can use inner join. You can skip the ifnull too, because both f and b will have an id.

紫罗兰の梦幻 2024-11-15 23:03:15

如果foobar之间的关系是一对一的并且双方都需要:

INSERT INTO foobar (
  SELECT f.fc_primary_key, f.fc_two, b.bc_three 
  FROM foo f INNER JOIN bar b ON f.fc_primary_key = b.bc_primary_key);

If the relationship between foo and bar is one-to-one and required on both sides:

INSERT INTO foobar (
  SELECT f.fc_primary_key, f.fc_two, b.bc_three 
  FROM foo f INNER JOIN bar b ON f.fc_primary_key = b.bc_primary_key);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文