我想将一个现有的 MySQL 表复制到一个重复的表中,但有两个额外的字段由从其他查询中检索到的数据填充。它适用于从未捕获线程的原始创建日期的论坛软件(它总是用最新的回复覆盖时间)。
所以我想使用现有表“threads”
thread_id
thread_author
thread_subject
thread_last_reply_date
并创建一个具有相同结构的新表“new_threads”,但有两个额外的字段:
thread_id
thread_author
thread_subject
thread_last_reply_date
thread_creation_date
thread_last_poster_id
thread_last_reply_date 和 thread_last_poster_id 都可以从相关查询中填充。例如,last_poster_id 查询:
SELECT post_author FROM posts WHERE thread_id = ? AND post_date = thread_last_reply_date
对于 thread_creation_date:
SELECT MIN(post_date) FROM posts WHERE thread_id = ?
这基本上就是我使用 PHP 脚本执行的过程。一系列 SELECT,然后一条一条地插入记录。关于如何在纯 SQL 中执行此类过程的任何建议或指导都将非常有用(如果可能的话)。
编辑:该技术的示例会有所帮助。我不需要以上的确切答案。我相信每个人都有更好的事情要做。
最佳答案
刚刚遇到了类似的问题。
这个解决方案对我有用:
INSERT INTO `new_thread` SELECT *,null,null FROM `thread` WHERE 1=1
关于mysql - 复制 MySQL 表并填充其他字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6172884/