How to Retrieve Number of Rows Affected in MySQL Queries Executed from Bash
When executing MySQL queries or commands from bash, it's often useful to know how many rows are affected by the operation. This information can be valuable for debugging, logging, or tracking progress.
Using the following syntax, you can execute MySQL queries from bash:
<code class="bash">mysql -u[user] -p[pass] -e "[mysql commands]"</code>
or
<code class="bash">mysql -u[user] -p[pass] `<<`QUERY_INPUT [mysql commands] QUERY_INPUT</code>
However, these methods do not return the number of affected rows directly.
To capture this information, you can add SELECT ROW_COUNT(); as the last statement in your batch. The output will then include the number of rows affected. You can parse this output to extract the desired data.
For example, to count the number of rows updated by a query, you could run:
<code class="bash">mysql -u[user] -p[pass] -e "[update query];SELECT ROW_COUNT();"</code>
The output of this command would include the number of rows updated, which you can then parse as needed.
The above is the detailed content of How to Get the Number of Affected Rows in MySQL Queries Run from Bash?. For more information, please follow other related articles on the PHP Chinese website!