tags:

views:

64

answers:

2

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 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

A: 

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?

Lukáš Lalinský
yes because these years are displayed into drop-downs into my system
Neveen
it cause that error "Cannot add or update a child row: a foreign key constraint fails (`CMMS/member`, CONSTRAINT `member_ibfk_9` FOREIGN KEY (`year`) REFERENCES `All_years_tmp` (`id`) ON DELETE CASCADE)" how can enforce to update ??
Neveen
The `year` columns references `all_years_tmp`, not `all_years`. To get a list of years for your drop-down menu you can use `SELECT DISTINCT year(birthday) FROM members`, not need to maintain a separate table.
Lukáš Lalinský
Thanks it works fine.
Neveen
A: 
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.

junmats
it cause the same error "ERROR 1093 (HY000): You can't specify target table 'member' for update in FROM clause "
Neveen