Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL - Update table changing one column value based on another table
    primarykey
    data
    text
    <p>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 ;).</p> <p>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.</p> <p>My tables look something like:</p> <p><strong>RelationTable</strong></p> <p>child-id</p> <p>parent-id</p> <p><strong>ImageTable</strong></p> <p>object-id</p> <p>attribute-id</p> <p>image-url</p> <p>And here goes an example in order to clarify things:</p> <p><strong>RelationsTable</strong></p> <p>child-id | parent-id</p> <p>3 | 1</p> <p>4 | 1</p> <p>5 | 2</p> <p><strong>ImageTable</strong></p> <p>object-id | attribute-id | image-url</p> <p>1 | goodimage | image1.jpg</p> <p>1 | badimage | image1b.jpg</p> <p>2 | goodimage | image2.jpg</p> <p>2 | badimage | image2b.jpg</p> <p>3 | goodimage | no</p> <p>3 | badimage | no</p> <p>4 | goodimage | no</p> <p>4 | badimage | no</p> <p>5 | goodimage | no</p> <p>5 | badimage | no</p> <p>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.</p> <p>At the end it should look like:</p> <p>1 | goodimage | image1.jpg</p> <p>1 | badimage | image1b.jpg</p> <p>2 | goodimage | image2.jpg</p> <p>2 | badimage | image2b.jpg</p> <p>3 | goodimage | image1.jpg</p> <p>3 | badimage | no</p> <p>4 | goodimage | image1.jpg</p> <p>4 | badimage | no</p> <p>5 | goodimage | image2.jpg</p> <p>5 | badimage | no</p> <p>Actually, I don't care if 'badimage' is set as well, but the important one is 'goodimage'.</p> <p>I've been trying something like:</p> <pre><code>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' </code></pre> <p>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.</p> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload