开发者

mysql - update foreign key with a value

开发者 https://www.devze.com 2022-12-08 16:50 出处:网络
I have a birthdate, year, month, day columns where columns \"year,month,day\" are foreign key to other tables

I have a birthdate, year, month, day columns where columns "year,month,day" are foreign key to other tables What I want to do is for each birthdate get id(year(birthdate)) to be the value of year column and the same thing for month and day columns.

How can I do this in 开发者_JAVA技巧MySQL?

i tried this solution:

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

but it cause " ERROR 1093 (HY000): You can't specify target table 'member' for update in FROM clause "

Thanks in advance


You don't want to select from the members table in the subquery. Use the table you are updating instead.

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

Is there a reason why year/month/date are foreign keys though?


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;

same goes for month and day.

0

精彩评论

暂无评论...
验证码 换一张
取 消