复制MySQL数据并使用条件创建新记录

时间:2013-02-18 13:40:54

标签: mysql sql sql-insert

最初我必须从旧表中获取ip并将其放入新表中,电子邮件在这两个表中都匹配。

INSERT INTO new (ip)
SELECT old.ip 
FROM old
INNER JOIN new on old.email = new.email;

我现在需要更改此查询以执行以下操作:

从旧表中获取ip 将它与新表格中该行的电子邮件相匹配 从新表中获取user_id
使用user_id和ip

在组合表中创建记录

到目前为止的查询:

这是否接近?似乎不对:/

comb_uid = SELECT old.ip FROM old  
WHERE new.email = old.email

comb_ip = SELECT new.user_id FROM new  

INSERT INTO combined (user_id, ip)
VALUES comb_uid, comb_ip 

1 个答案:

答案 0 :(得分:0)

你已经够近了。只需将new更改为combined

INSERT INTO combined (ip, user_ID)
SELECT  old.ip, new.user_ID
FROM    old
        INNER JOIN new 
            on old.email = new.email;

更新1

INSERT INTO combined (ip, user_ID, status)
SELECT  old.ip, new.user_ID, 'approved'
FROM    old
        INNER JOIN new 
            on old.email = new.email;