MYSQL INSERT INTO 现有表数据 FROM 2 个单独的表 ON id
我有一个空表“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只需要两个表中都存在的记录,则可以使用内连接。您也可以跳过 ifnull,因为 f 和 b 都会有一个 id。
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.
如果
foo
和bar
之间的关系是一对一的并且双方都需要:If the relationship between
foo
andbar
is one-to-one and required on both sides: