Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL, multiple table update and strange result
    primarykey
    data
    text
    <p>I've got two tables :</p> <p>TEST1 : </p> <pre><code>id int, value int, parentId int </code></pre> <p>and TEST2:</p> <pre><code>id int, value int, parentId int </code></pre> <p>In TEST1, I've got following records:</p> <pre><code> id value parentId 1 0 1 2 0 1 3 0 1 </code></pre> <p>in TEST2, I've got:</p> <pre><code>id value parentId 1 0 1 </code></pre> <p>I want to update the two tables in one update using multiple table feature. The goal is to add +1 value to each records of TEST1 and +1 value to the record in TEST2 where the parentId is similar to TEST1.</p> <p>My query is :</p> <pre><code>UPDATE TEST1 t1 LEFT OUTER JOIN TEST2 t2 ON t1.parentId=t2.parentId SET t1.value = t1.value + 1, t2.value = t2.value + 1; </code></pre> <p>After that, I do a select to check that values of TEST1 have been updated :</p> <pre><code>SELECT * FROM TEST1; </code></pre> <p>It gives me : </p> <pre><code>id value parentId 1 1 1 2 1 1 3 1 1 </code></pre> <p>I check TEST2 :</p> <pre><code>SELECT * FROM TEST2; </code></pre> <p>It gives me :</p> <pre><code>id value parentId 1 1 1 </code></pre> <p>What I find strange is that the record of TEST2 has a value of 1. I would expect 3 because the update of TEST1 is on 3 records and so the update of TEST2 should occurs three times on the same record.</p> <p>Why I obtain 1 instead of 3 ? and What is the correct query to obtain 3 ?</p> <p>EDIT: I've also tried : </p> <pre><code>SET @var=1; UPDATE TEST1 t1 LEFT OUTER JOIN TEST2 t2 ON t1.parentId=t2.parentId SET t1.value = t1.value + 1, t2.value = (@var:= @var + 1); </code></pre> <p>After this query, t2.value is equal to 2 ! And not 4 as I would expect.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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