Hi. Sorry if the title is not as descriptive as it should be but it is kind of difficult to explain in one sentence what I am trying to do ;).
I have one table that links parent objects with its respective childs. And I have another table with all the objects (parents and childs) with its respectives images. However, the image is just set for the parents objects. I would like to update this last table and set the childs image the same image that is already set for its parent. Besides, as there is more than one image for each object, I would like to set one in particular, which I can know based on an attribute column.
My tables look something like:
RelationTable
child-id
parent-id
ImageTable
object-id
attribute-id
image-url
And here goes an example in order to clarify things:
RelationsTable
child-id | parent-id
3 | 1
4 | 1
5 | 2
ImageTable
object-id | attribute-id | image-url
1 | goodimage | image1.jpg
1 | badimage | image1b.jpg
2 | goodimage | image2.jpg
2 | badimage | image2b.jpg
3 | goodimage | no
3 | badimage | no
4 | goodimage | no
4 | badimage | no
5 | goodimage | no
5 | badimage | no
So, I would like to set the images of objects 3, 4 and 5 (child ones) to its respective parent images, but to the 'correct' ones, that is the images with 'goodimage' as attribute-id.
At the end it should look like:
1 | goodimage | image1.jpg
1 | badimage | image1b.jpg
2 | goodimage | image2.jpg
2 | badimage | image2b.jpg
3 | goodimage | image1.jpg
3 | badimage | no
4 | goodimage | image1.jpg
4 | badimage | no
5 | goodimage | image2.jpg
5 | badimage | no
Actually, I don't care if 'badimage' is set as well, but the important one is 'goodimage'.
I've been trying something like:
UPDATE ImageTable
SET image = (SELECT image-url FROM ImageTable WHERE ImageTable.object-id = RelationTable.parent-id AND ImageTable.attribute-id = 'goodimage')
WHERE ImageTable.object-id = RelationTable.child-id AND ImageTable.attribute-id = 'goodimage'
but it's not working since it is not correct SQL syntax. I don't know if I should use a variable (never used one) or if this can be done with just one SQL sentence.
Any help would be much appreciated.