When executing MySQL queries through the bash command line, you can retrieve the number of rows affected by the query using a specific technique.
To obtain this information, you can incorporate the SELECT ROW_COUNT(); statement as the concluding part of your SQL command block. By doing this, you enable the query to output the number of affected rows.
To capture this output in bash, you can use backticks () or the heredoc ( << QUERY_INPUT `) syntax. However, you'll need to parse the output to isolate the row count value.
For example, to execute the UPDATE query and obtain the affected row count, you would use:
<code class="bash">variable=$(mysql -u[user] -p[pass] -e "UPDATE table_name SET column_name = 'new_value' WHERE condition; SELECT ROW_COUNT();")</code>
Once you have the output stored in the $variable, you can retrieve the row count using appropriate string manipulation techniques, such as grep or sed.
The above is the detailed content of How to Capture the Number of Rows Affected by a MySQL Query from Bash?. For more information, please follow other related articles on the PHP Chinese website!