如何将数据从1个表复制到另一个具有不同结构的表

时间:2018-01-18 11:17:38

标签: mysql database-migration insert-update

我有一个结构

的表
CREATE TABLE `old_reminder` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `applicant_id` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `type` enum('payment_15_min','payment_1_day','payment_3_day') NOT NULL DEFAULT 'payment_15_min',
  PRIMARY KEY (`id`)
)

我希望将其数据迁移到另一个具有结构

的表
CREATE TABLE `new_reminders` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `payment_reminder_1_count` int(11) DEFAULT NULL,
  `payment_reminder_1_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

每个user_id基本上多行将转换为1行,所有枚举值都作为列。 我尝试了以下但它只更新了一行

UPDATE reminders 
    INNER JOIN old_reminder AS `old`
    ON user_id = old.applicant_id
    SET new_reminder_1_date = IF(old.type = 'payment_15_min' OR old.type = 'payment_1_day' OR old.type = 'payment_3_day', old.date, '2018-01-01 00:00:00'),
    payment_reminder_1_count = IF(old.type = 'payment_15_min' OR old.type = 'payment_1_day' OR old.type = 'payment_3_day',
    CASE
    WHEN old.type = 'payment_15_min' THEN 1
    WHEN old.type = 'payment_1_day' THEN payment_reminder_1_count + 1
    WHEN old.type = 'payment_3_day' THEN payment_reminder_1_count + 1 END, 0)
    WHERE applicant_id = 123;

1 个答案:

答案 0 :(得分:0)

如果你的模特看起来像这样

truncate table old_reminder;
insert into old_reminder (applicant_id,date,type) values (123,'2018-01-01','payment_15_min');
insert into old_reminder (applicant_id,date,type) values (123,'2018-01-02','payment_1_day');
truncate table new_reminders;
insert into new_reminders(user_id,payment_reminder_1_date) values (123,'2016-01-01');

然后是查询的简化版本

update new_reminders n join (select * from old_reminder order by applicant_id,`date`) old on old.applicant_id = n.user_id
     SET 
     enumstr = old.type,
     payment_reminder_1_date = 
     case 
        when old.type in ('payment_15_min','payment_1_day','payment_3_day') then old.date
        else '2017-01-01'   
     end     
    WHERE applicant_id = 123;

产生这个结果,我认为你的意思是它只更新了一行

+----+---------+--------------------------+-------------------------+----------------+
| id | user_id | payment_reminder_1_count | payment_reminder_1_date | enumstr        |
+----+---------+--------------------------+-------------------------+----------------+
|  1 |     123 |                     NULL | 2018-01-01 00:00:00     | payment_15_min |
+----+---------+--------------------------+-------------------------+----------------+
1 row in set (0.00 sec)

在此查询中最后评估where子句,并可以从结果集中自由选择它 - 在这种情况下始终是第一个。

如果更改查询以测试support_reminder_1_date

update new_reminders n join (select * from old_reminder order by applicant_id,`date`) old on old.applicant_id = n.user_id
     SET 
     enumstr = old.type,
     payment_reminder_1_date = 
     case 
        when old.type in ('payment_15_min','payment_1_day','payment_3_day') then old.date
        else '2017-01-01'   
     end     
    WHERE applicant_id = 123 and n.payment_reminder_1_date <> old.`date`

然后你会得到更像你希望的东西

+----+---------+--------------------------+-------------------------+---------------+
| id | user_id | payment_reminder_1_count | payment_reminder_1_date | enumstr       |
+----+---------+--------------------------+-------------------------+---------------+
|  1 |     123 |                     NULL | 2018-01-02 00:00:00     | payment_1_day |
+----+---------+--------------------------+-------------------------+---------------+
1 row in set (0.00 sec)

但我认为这实际上并不是你想要的,我建议光标可能是前进的方向。