php - 迁移旧表数据到新表查询

标签 php mysql sql

需要一些帮助来创建一个查询,该查询从 mb_topics 获取 topic_titletopic_content,以及 post_contentmb_posts 表中插入 mb_topics.topic_titleforum_topics.topic_title (table.field), mb_topics.topic_content mb_posts.post_contentforum_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;

论坛 表格:

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;
}

最佳答案

我想像这样的东西可以吗?

<?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')");
}

关于php - 迁移旧表数据到新表查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32468873/

相关文章:

php - 通过 PHP 从 ajax 访问 MySQL 数据库

mysql - 使用 Ruby on Rails 进行 count_at_date

mysql - 检查两篇文章之间是否有共同的标签

sql - 将多行合并为一个空格分隔的字符串

php - CakePHP mysql 选择月份中的某一天

ios - 仅从服务器获取已更改的 SQL 结果

mysql - 数据库查询永远加载

php - 将 html 格式的文本保存到数据库

php - 为什么我的递归函数不起作用?

php - 在php中的mysql语句中连接4个表