kcott raised the most important points. To expand on that a bit...

The first time you execute an SQL statement like this:

update table set ID = 10 where ID = 5"
that one execution will cause every row with ID=5 to updated to ID=10. If you execute the exact same statement a second time (e.g. trying to set ID=11 instead of 10), nothing will happen, because you no longer have any rows with ID=5 (all those rows now have ID=10).

I would suggest using a SELECT statement to get all the rows having ID=5, and then for each row returned by that query, do an update with desired new value for ID:

my $update_sth = $dbh->prepare( "update table set ID=? where ID=5 and +Name=?"); my $select_sth = $dbh->prepare( "select Name from table where ID=5" ); my $new_ID = 10; $select->execute; while ( my $row = $select->fetchrow_arrayref ) { my $name = $$row[0]; $update_sth->execute( $new_ID++, $name ); }
(You should probably add some error checking, to make sure the updates work as intended.)

Updated to fix a typo in the 2nd line of the code snippet (was "IS=5") -- thanks kcott!


In reply to Re: Increase a value inside MySQL query using perl by graff
in thread Increase a value inside MySQL query using perl by AhmedABdo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.