I have 3 tables in this format:
Table Child:
id|parent_id|grandparent_id
1 1 null
2 2 null
3 3 null
Table Parent:
id|grandparent_id
1 1
2 1
3 2
Table GrandParent:
id
1
2
I need to run a query that updates grandparent_id column in Child table based on the grandparent_id in Parent table. So the correct final form of Child table will be: Table Child:
id|parent_id|grandparent_id
1 1 1
2 2 1
3 3 2
This is the query I have at the moment but it returns more than 1 row which is wrong:
update child set grandparent_id = (
select gpd.id from GrandParent gp, Parent p
where p.grandparent_id = gp.id)
where 1