mysql - 用值更新外键

时间:2009-10-19 07:28:58

标签: mysql sql mysql-error-1093

我有一个生日,年,月,日列,其中“年,月,日”列是其他表的外键 我想要做的是每个生日获取ID(年(生日))为年份列的值,月和日列的值相同。

我如何在MySQL中执行此操作?

我试过这个解决方案:

update member set year=(select All_years.id from All_years,member where All_years.fromY=year(member.birthdate)) where id=30471;

但它会导致“ERROR 1093(HY000):您无法在FROM子句中为更新指定目标表'成员'”

提前致谢

2 个答案:

答案 0 :(得分:0)

您不想从子查询中的members表中进行选择。请改用您正在更新的表格。

UPDATE member
SET year=(
   SELECT id FROM all_years
   WHERE fromY=year(member.birthdate)
)
WHERE id=30471;

有没有理由说年/月/日是外键?

答案 1 :(得分:-1)

SELECT birthdate FROM member INTO @myBirthdate;

update member set year=(select All_years.id from All_years,member where All_years.fromY=year(@myBirthdate)) where id=30471;

同样适用于月和日。