1

这是我要复制的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)
4

1 回答 1

0

您确实应该发布您的表创建语句,但您似乎也误解了ON UPDATE语法。

对于 on update,您应该明确设置要更新的列,这似乎是question_text列。您还需要设置哪些列需要用于UNIQUE约束。

CREATE TABLE IF NOT EXISTS new_schema.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 ;



CREATE TABLE `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`),
  UNIQUE KEY `unique_quizID_priority` (`quiz_id`,`priority`),
  KEY `quiz_id` (`quiz_id`),
  KEY `question_id` (`question_id`)
) ENGINE=InnoDB AUTO_INCREMENT=180 DEFAULT CHARSET=latin1;





iNSERT INTO new_schema.questions ( `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
(' Letter A', 1, 1, 1, 1, '2013-03-06 09:12:58', 0, NULL, NULL, '', '', NULL, NULL),
(' Letter B', 1, 2, 1, 1, '2013-03-06 09:13:21', 0, NULL, NULL, '', '', NULL, NULL),
('letter C', 1, 3, 1, 1, '2013-03-06 13:43:48', 0, NULL, NULL, '', '', NULL, NULL);




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_text = questions.question_text;



iNSERT INTO new_schema.questions ( `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
( 'Letter D', 1, 1, 1, 1, '2013-03-06 09:12:58', 0, NULL, NULL, '', '', NULL, NULL),
( 'Letter E', 1, 2, 1, 1, '2013-03-06 09:13:21', 0, NULL, NULL, '', '', NULL, NULL),
( 'letter F', 1, 3, 1, 1, '2013-03-06 13:43:48', 0, NULL, NULL, '', '', NULL, NULL);



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_text = questions.question_text;


select * from new_schema.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 182,22,”字母 D”,1,1,1,1 ,"2013-03-08 01:11:36",0,0,0,,,, 183,23,"字母 E",1,2,1,1,"2013-03-08 01:11: 36",0,0,0,,,, 184,24,"字母 F",1,3,1,1,"2013-03-08 01:11:36",0,0,0,,, ,

显示的文本已被覆盖。如果要更新其他列,则需要在ON UPDATE查询部分中设置它们。

此外,您应该(几乎)永远不要将自动增量值插入到表中。让 MySQL 为您解决这个问题。此外,您在 testbank_question 表的“id”中有一个空格类型。

于 2013-03-07T03:54:25.780 回答