将一个表复制到另一个表加上一列依赖于另一列

时间:2015-01-15 16:55:15

标签: mysql sql insert-into

NAME和VER是主键。 我会画画,而不是写下我想做的事情。

表来源:

NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME
john | bob        | 1   | doe        | 18-AUG-14 10.16.08.695221100 AM
john | bob        | 2   | doe        | 19-AUG-14 11.01.42.109826000 AM
john | bob        | 3   | doe        | 09-SEP-14 10.14.15.929342100 AM
john | bob        | 4   | doe        | 10-SEP-14 10.14.15.929342100 AM

表目标:

NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME                       | DATEUP
john | bob        | 1   | doe        | 18-AUG-14 10.16.08.695221100 AM | 19-AUG-14 11.01.42.109826000 AM
john | bob        | 2   | doe        | 19-AUG-14 11.01.42.109826000 AM | 09-SEP-14 10.14.15.929342100 AM
john | bob        | 3   | doe        | 09-SEP-14 10.14.15.929342100 AM | 10-SEP-14 10.14.15.929342100 AM
john | bob        | 4   | doe        | 10-SEP-14 10.14.15.929342100 AM | 31-DEC-99 0.0.0.0 AM

感谢。

1 个答案:

答案 0 :(得分:0)

使用相同的表格执行OUTER JOIN以获得结果

SELECT a.NAME,
       a.something,
       a.VER,
       a.something,
       a.MODDATIME,
       Ifnull(b.MODDATIME,'31-DEC-99 0.0.0.0 AM') AS DateUP
FROM   Yourtable a
       LEFT OUTER JOIN Yourtable b
              ON a.NAME = b.NAME
                 AND a.VER = b.VER - 1