Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depending on the method of execution used you usually can get the number of rows affected by any write operation performed on the my sql database. There are multiple ways, you can explicitly execute the statement as a batch with </p> <pre><code>SELECT ROW_COUNT() </code></pre> <p>at the end or you can use the features provided by the connection library to get the number of rows affected in the last set</p> <p>For example : in PHP you can use</p> <pre><code>mysqli::$affected_rows </code></pre> <p>for getting the affected rows.</p> <p>Edit : Is there any particular scenario, you would like more examples on?</p> <p>Update : To answer the second part of the question.</p> <p>Well, there is absolutely no way the you can get the primary keys of the affected rows in the same statement that I know. You will need to execute a statement batch as mentioned in the answer examples given by the other user.</p> <del> START TRANSACTION; SELECT primary_key_columns FROM my_table WHERE status = 'O' FOR UPDATE; UPDATE my_table SET status = 'E' WHERE status = 'O'; COMMIT; More detail here : [https://stackoverflow.com/questions/10663338/i-need-primary-keys-of-the-affected-rows-to-be-returned-after-updating-a-table-i][1] </del> <p><br /> EDIT Modified the answer based on info given by Marcus in the comments below. </p> <blockquote> <p>To retrieve the ID of every row affected by an update statement:</p> </blockquote> <pre><code>SET @uids := null; UPDATE footable SET foo = 'bar' WHERE fooid &gt; 5 AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) ); SELECT @uids; </code></pre> <p>This will return a string with all the IDs concatenated by a colon. Source :<a href="https://stackoverflow.com/questions/1388025/how-to-get-id-of-the-last-updated-row-in-mysql#1751282">How to get ID of the last updated row in MySQL?</a></p> <p>The other option as mentioned by the answer given by @fenway is the output clauses for supported database.</p> <p>Note that even in that case you essentially <strong>have to</strong> fill in the values before you send the data back following the same step mentioned above.</p> <p>Now, I am sure, you are wondering why is it that the database do not return primary key values automatically. Well it has to do with the fact that</p> <p>1) A DB does not insist on having a Primary key for each and every table. Its actually a UNIQUE constraint defined by us.</p> <p>2) Not all scenarios do you need to have the affected primary keys returned . In most of the usual use-cases, after update you require at most the rows affected.</p> <p>Remember, Relational DB programming requires a slightly different mindset from the usual problem set programming. </p> <p>Atleast these are my two cents.</p> <p>Regards</p> <p>Shreyas N</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.
    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