我试图将一些数据从一个表复制到另一个电子邮件地址相同的表。
例如:
表 1 包含以下字段:
email, title, first_name, last_name, (+ others)
表 2 包含以下字段:
email, title, first_name, last_name, modified, (+ others)
我想将title
, first_name
, last_name
from复制table2
到table1
where table1.email = table2.email
。
所以table2
保存了所有数据,我想将这些数据复制到table1
电子邮件与 intable1
匹配的FIRST 'most recent (by 'modified')' email found
位置table2
(因此是最近修改的datetime
)。
我正在尝试这样的事情:
INSERT INTO `table1` (title, first_name, last_name)
SELECT title, first_name, last_name from `table2`
WHERE table1.email = table2.email
ERROR: Unknown column 'table1.email' in 'where clause'
所以基本上,我在一个声明中需要这样的东西..
SELECT EMAIL as `originalEmail` FROM `table1`
SELECT title, first_name, last_name FROM `table2`
WHERE table2.email = table1.email
ORDER BY `modified` desc # Get the most recently modified
LIMIT 1 # limit to 1 result (the most recent one)
UPDATE `table1` (title, first_name, last_name) values (*values from above*)
WHERE EMAIL = `originalEmail`
编辑 - 我想UPDATE
而不是INSERT
新记录。