MySQL:合并两个表(相同架构)并创建一个新表 - 单个查询
我有两个具有完全相同架构的表。
我想要一个第三个表,包含这两个表的所有数据。
如何使用 INSERT INTO 查询(单个查询执行此操作)来执行此操作?
我知道我可以这样做: INSERT INTO
name_of_new_tableSELECT DISTINCT * FROM
name_of_old_table 但随后我需要执行两次。我正在寻求通过单个查询来完成此操作。
谢谢。
I have two tables with exactly the same schema.
I would like to have a 3rd table, containing all the data of these two tables combined.
How can I do this with an INSERT INTO query (single query doing this)?
I know I can do this with: INSERT INTO
name_of_new_tableSELECT DISTINCT * FROM
name_of_old_table but then I would need to do it twice. I am seeking to doing this with a SINGLE query.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除了您的
DISTINCT
,因为UNION
有一个隐含的DISTINCT
(UNION ALL
没有这个)。然而,只有当表的列数相同并且具有相似的列类型时,这才有效。再说一次,听起来这就是你的情况。Removed your
DISTINCT
, asUNION
has an impliedDISTINCT
(UNION ALL
does not have this). This will only work if the tables are identical in column count and have similar column type, however. Then again, it sounds like that's your situation.