这是我要复制的mysql数据
表“问题”
id|question_text |priority|quiz_id|
1 |<p>Letter D</p>| 1 | 1 |
2 |<p>Letter B</p>| 2 | 1 |
3 |<p>Letter C</p>| 3 | 1 |
然后这是我的新表我已经有两个问题表'testbank_question'
id|question_id|question_text |priority|quiz_id|
89| 1 |<p>Letter A</p>| 1 | 1 |
90| 2 |<p>Letter B</p>| 2 | 1 |
当我更新时,我希望结果在我的新表“testbank_question”中看起来像这样
id |question_id|question_text |priority|quiz_id|
89 | 1 |<p>Letter D</p>| 1 | 1 |
90 | 2 |<p>Letter B</p>| 2 | 1 |
150| 3 |<p>Letter C</p>| 3 | 1 |
--table 'questions' 创建 statemate
CREATE TABLE IF NOT EXISTS `questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_text` varchar(3800) DEFAULT NULL,
`question_type_id` int(11) NOT NULL,
`priority` int(11) NOT NULL,
`quiz_id` int(11) NOT NULL,
`point` decimal(18,0) NOT NULL,
`added_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`parent_id` int(11) NOT NULL,
`question_total` decimal(18,0) DEFAULT NULL,
`check_total` int(11) DEFAULT NULL,
`header_text` varchar(1500) CHARACTER SET utf8 DEFAULT NULL,
`footer_text` varchar(1500) CHARACTER SET utf8 DEFAULT NULL,
`question_text_eng` varchar(1800) CHARACTER SET utf8 DEFAULT NULL,
`help_image` varchar(550) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `quiz_id` (`quiz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `questions` (`id`, `question_text`, `question_type_id`, `priority`, `quiz_id`, `point`, `added_date`, `parent_id`, `question_total`, `check_total`, `header_text`, `footer_text`, `question_text_eng`, `help_image`) VALUES
(1, '<p>\r\n Letter A</p>', 1, 1, 1, 1, '2013-03-06 09:12:58', 0, NULL, NULL, '', '', NULL, NULL),
(2, '<p>\r\n Letter B</p>', 1, 2, 1, 1, '2013-03-06 09:13:21', 0, NULL, NULL, '', '', NULL, NULL),
(3, '<p>\r\n letter C</p>', 1, 3, 1, 1, '2013-03-06 13:43:48', 0, NULL, NULL, '', '', NULL, NULL);
ALTER TABLE `questions`
ADD CONSTRAINT `questions_ibfk_1` FOREIGN KEY (`quiz_id`) REFERENCES `quizzes` (`id`) ON DELETE CASCADE;
--
-- 表的表结构testbank_question
CREATE TABLE IF NOT EXISTS `testbank_question` (
` id` int(100) NOT NULL AUTO_INCREMENT,
`question_id` int(100) NOT NULL,
`question_text` varchar(3800) NOT NULL,
`question_type_id` int(11) NOT NULL,
`priority` int(11) NOT NULL,
`quiz_id` int(11) NOT NULL,
`point` decimal(18,0) NOT NULL,
`added_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`parent_id` int(11) NOT NULL,
`question_total` decimal(18,0) NOT NULL,
`check_total` int(11) NOT NULL,
`header_text` varchar(1500) NOT NULL,
`footer_text` varchar(1500) NOT NULL,
`question_text_eng` varchar(1800) NOT NULL,
`help_image` varchar(550) NOT NULL,
PRIMARY KEY (` id`),
KEY `quiz_id` (`quiz_id`),
KEY `question_id` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=176 ;
--
-- 为表转储数据testbank_question
INSERT INTO `testbank_question` (` id`, `question_id`, `question_text`, `question_type_id`, `priority`, `quiz_id`, `point`, `added_date`, `parent_id`, `question_total`, `check_total`, `header_text`, `footer_text`, `question_text_eng`, `help_image`) VALUES
(89, 1, '<p>\r\n Letter A</p>', 1, 1, 1, 1, '2013-03-06 09:12:58', 0, 0, 0, '', '', '', ''),
(90, 2, '<p>\r\n Letter B</p>', 1, 2, 1, 1, '2013-03-06 09:13:21', 0, 0, 0, '', '', '', '');
--
-- 表格约束testbank_question
ALTER TABLE `testbank_question`
ADD CONSTRAINT `testbank_question_ibfk_1` FOREIGN KEY (`quiz_id`) REFERENCES `testbank_quizzes` (`copy_id`) ON DELETE CASCADE;
在我的新表中,字段 question_id 等于我要复制的数据的 id。这是我在 PHP 中的代码
INSERT INTO testbank_question
(question_id,
quiz_id,
question_text,
question_type_id,
priority,point,
added_date,
parent_id,
question_total,
check_total,
header_text,
footer_text,
question_text_eng,
help_image)
SELECT id,
quiz_id,
question_text,
question_type_id,
priority,
point,
added_date,
parent_id,
question_total,
check_total,
header_text,
footer_text
,question_text_eng,
help_image
FROM questions WHERE
questions.quiz_id='1' ON DUPLICATE KEY UPDATE
testbank_question.question_id = VALUES(testbank_question.question_id)