需要一些帮助来创建一个查询,该查询从topic_title
获取topic_content
和mb_topics
,从post_content
表中获取mb_posts
然后插入将mb_topics.topic_title
放入forum_topics.topic_title
(table.field),mb_topics.topic_content
和mb_posts.post_content
放入forum_posts
表。
是否可以使用单个选择查询?
mb
表:
CREATE TABLE IF NOT EXISTS `mb_posts` (
`id` int(11) NOT NULL,
`posted_by` bigint(20) NOT NULL DEFAULT '0',
`topic_id` int(11) NOT NULL DEFAULT '0',
`post_content` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `mb_topics` (
`id` int(11) NOT NULL,
`topic_title` varchar(75) NOT NULL DEFAULT '',
`posted_by` bigint(20) NOT NULL DEFAULT '0',
`topic_content` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
forum
表:
CREATE TABLE IF NOT EXISTS `forum_posts` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`post_content` text NOT NULL,
`post_date` datetime NOT NULL,
`post_topic` int(11) NOT NULL,
`post_by` int(11) unsigned NOT NULL
PRIMARY KEY (`post_id`),
KEY `post_topic` (`post_topic`),
KEY `post_by` (`post_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `forum_topics` (
`topic_id` int(11) NOT NULL AUTO_INCREMENT,
`topic_title` varchar(150) NOT NULL,
`topic_by` int(11) unsigned NOT NULL
PRIMARY KEY (`topic_id`),
KEY `topic_by` (`topic_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
PHP:
$get_old_data_query=mysql_query('SELECT `title`, `content` FROM `mb_topics`');
while($old_data=mysql_fetch_assoc($get_old_data_query))
{
$old_data_array[]=$old_data;
}
答案 0 :(得分:2)
我觉得这样的东西会好吗?
<?php
$Old = mysql_query("SELECT * FROM Table WHERE Name = 'Name'");
while(mysql_fetch_array($Old)){
$Data = mysql_fetch_array($Old);
$Old1 = $Data['Name'];
mysql_query("INSERT INTO NewTable (Name) VALUES ('$Old1')");
}
答案 1 :(得分:0)
以下查询将从 mb_posts 和 mb_topics 表中获取数据,并将插入 forum_posts 表中。
INSERT INTO forum_posts( post_content, post_topic, post_date, post_by )
SELECT MBP.post_content, MBT.id, NOW( ) , MBP.posted_by
FROM mb_posts AS MBP
LEFT JOIN mb_topics AS MBT ON MBP.topic_id = MBT.id
类似地,以下查询将从旧表(mb_topics)到新表(forum_topics)发布帖子主题。
INSERT INTO forum_topics( topic_title, topic_by)
SELECT MBT.topic_title, MBT.posted_by
FROM mb_posts AS MBP
LEFT JOIN mb_topics AS MBT ON MBP.topic_id = MBT.id
答案 2 :(得分:-1)
这将是2个单独的查询,因为只有一个INSERT,您只能插入1个表。