2

我有一个生日、年、月、日列,其中列“年、月、日”是其他表的外键我想要做的是为每个生日获取 id(year(birthdate)) 作为年列的值月和日列也是如此。

我怎样才能在 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): You can't specify target table 'member' for update in FROM 子句”

提前致谢

4

2 回答 2

0

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

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

有没有为什么年/月/日是外键的原因?

于 2009-10-19T07:39:57.117 回答
-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;

一个月和一天也是如此。

于 2009-10-19T07:46:22.563 回答