Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制MySQL表并填充其他字段_Mysql_Select_Insert - Fatal编程技术网

复制MySQL表并填充其他字段

复制MySQL表并填充其他字段,mysql,select,insert,Mysql,Select,Insert,我想将一个现有的MySQL表复制到一个重复的表中,但另外两个字段由从其他查询检索到的数据填充。这是一个论坛软件,从来没有捕捉到线程的原始创建日期(它总是用最新的回复重写时间) 因此,我想使用现有的表“threads” thread_id thread_author thread_subject thread_last_reply_date 并创建一个新表,“new_threads”,其结构相同,但有两个额外字段: thread_id thread_author thread_subject

我想将一个现有的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 
可以通过相关查询填充线程\u last\u reply\u date和线程\u last\u poster\u id。。例如,查询为的最后一个海报id:

SELECT post_author FROM posts WHERE thread_id = ? AND post_date = thread_last_reply_date
对于线程创建日期:

SELECT MIN(post_date) FROM posts WHERE thread_id = ?
这基本上就是我用PHP脚本所做的过程。。一系列选择,然后逐个插入记录。任何关于如何在纯SQL中执行这类过程的建议或指导都会非常有用(如果可能的话)


编辑:举个技术的例子会很有帮助。我不需要一个确切的答案。我相信每个人都有更好的事情要做。

要创建目标表(空):

要填充它,请执行以下操作:

insert into new_threads(thread_id, thread_author, thread_subject, thread_last_reply_date, thread_creation_date, thread_last_poster_id)
(select t.*, 
  min(p.post_date), 
  (SELECT p1.post_author 
   FROM posts p1 
   WHERE p1.thread_id = t.thread_id 
   AND p1.post_date = t.thread_last_reply_date) thread_last_poster_id
  from threads t, posts p
  where t.thread_id = p.thread_id;

(未经测试)

也有类似的问题

这个解决方案对我有效:

INSERT INTO `new_thread` SELECT *,null,null FROM `thread` WHERE 1=1
INSERT INTO `new_thread` SELECT *,null,null FROM `thread` WHERE 1=1